Java Program to Create and Set Border to Push Buttons

This is a Java program to create push buttons and draw different borders around the buttons.

Problem Description

We have to write a program in Java such that it creates a frame containing five Push Buttons and different types of border are drawn around it.

Expected Input and Output

To draw different borders around the buttons, we have the following set of input and output.

To display the borders:
At the execution of the program, it is expected that a frame appears with five push borders having different types of border around them.
Button 1 – LineBorder with black color and 2px size.
Button 2 – LineBorder with black color and 10px size.
Button 3 – Etched Raised Border with highlighting black color and white as the shadow color.
Button 4 – Etched Lowered Border with highlighting black color and white as the shadow color.
Button 5 – Bevel Raised Border with highlighting black color and white as the shadow color.

Problem Solution

1. Create a frame and push buttons.
2. Create different types of borders as follows :-
a) Thin Border: Create a border of class LineBorder of color black and size 2px.
b) Thick Border: Create a border of class LineBorder of color black and size 10px.
c) Etched Raised Border: Create a border of class EtchedBorder of type RAISED having highlight color black and shadow color white.
d) Etched Lowered Border: Create a border of class EtchedBorder of type LOWERED having highlight color black and shadow color white.
e) Bevel Raised Border: Create a border of class BevelBorder of type RAISED having highlight color black and shadow color white.

3. Add the different borders to buttons.
4. Display the frame.

advertisement
advertisement
Program/Source Code

Here is source code of the Java Program to draw different borders around buttons. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /*Java Program to draw borders around Push Button*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import javax.swing.border.*;
  5. class Button_Border
  6. {
  7.     //Driver function
  8.     public static void main(String args[])
  9.     {
  10.          //Create a frame
  11. 	 JFrame frame=new JFrame("Borders around Buttons");
  12. 	 frame.setSize(500,500);
  13. 	 frame.setLayout(null);
  14. 	 frame.setBackground(Color.white);
  15. 	 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. 	 //Create push buttons
  17. 	 JButton[] button=new JButton[5];
  18. 	 for(int i=0;i<5;i++)
  19. 	 {
  20. 	     button[i]=new JButton("Button "+(i+1));
  21. 	     button[i].setBounds(210,i*75,100,50);
  22. 	     frame.add(button[i]);
  23. 	 }
  24. 	 //Create different borders
  25.          LineBorder thin,thick;
  26.          EtchedBorder raised,low;
  27.          BevelBorder bevel;
  28. 	 thin=new LineBorder(Color.black,2);
  29. 	 thick=new LineBorder(Color.black,10);
  30. 	 raised=new EtchedBorder(EtchedBorder.RAISED,Color.black,Color.white);
  31. 	 low=new EtchedBorder(EtchedBorder.LOWERED,Color.black,Color.white);
  32. 	 bevel=new BevelBorder(BevelBorder.RAISED,Color.black,Color.white);
  33. 	 //Add borders to buttons
  34. 	 button[0].setBorder(thin);
  35. 	 button[1].setBorder(thick);
  36. 	 button[2].setBorder(raised);
  37. 	 button[3].setBorder(low);
  38. 	 button[4].setBorder(bevel);
  39. 	 //Display frame
  40. 	 frame.setVisible(true);
  41.     }
  42. }
Program Explanation

1. Create a frame with background color & specific size. frame.setBackground(Color.white); is used to create the frame background color as white. frame.setSize(500,500); is used to set the width and height of the frame.
2. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); is a default close operation for the frame.
3. Create 5 push buttons using for loop and set bound values. Once push buttons are created add them to the frame by using frame.add();.
4. Create a different borders for each button using different border types like LineBorder (thin, thick), EtchedBorder (raised, low) & BevelBorder.
5. thin=new LineBorder(Color.black,2); –> It is used to create a border for LineBorder class with black color and 2px size. Thin Border is used for Button 1.
6. thick=new LineBorder(Color.black,10); –> It is used to create a border for LineBorder class with black color and 10px size. Thick Border is used for Button 2.
7. raised=new EtchedBorder(EtchedBorder.RAISED,Color.black,Color.white); –> It is used to create a border for EtchedBorder class with RAISED type which is having black color highlighting and white color shadow. Etched Raised Border is used for Button 3.

Note: Join free Sanfoundry classes at Telegram or Youtube

8. low=new EtchedBorder(EtchedBorder.LOWERED,Color.black,Color.white); –> It is used to create a border for EtchedBorder class with LOWERED type which is having black color highlighting and white color shadow. Etched LOWERED Border is used for Button 4.

9. bevel=new BevelBorder(BevelBorder.RAISED,Color.black,Color.white); –> It is used to create a border for BevelBorder class with RAISED type which is having black color highlighting and white color shadow. Bevel Raised Border is used for Button 5.

10. Once borders creation is done. Add borders to buttons as follows:
button[0].setBorder(thin); –> Thin Border is added for Button 1
button[1].setBorder(thick); –> Thick Border is added for Button 2.
button[2].setBorder(raised); –> Etched Raised Border is added for Button 3.
button[3].setBorder(low); –> Etched LOWERED Border is added for Button 4.
button[4].setBorder(bevel); –> Bevel Raised Border is added for Button 5.

11. frame.setVisible(true); –> It is used to display the frame.

advertisement
Runtime Test Cases

Here’s the run time test case to draw different borders around buttons.

Test case – Here’s the runtime output to display the borders around the buttons.
Button 1 displays Thin LineBorder with black color and 2px size.
Button 2 displays Thick LineBorder with black color and 10px size.
Button 3 displays Etched Raised Border with black color highlighting and white as the shadow color.
Button 4 displays Etched Lowered Border with black color highlighting and white as the shadow color.
Button 5 displays Bevel Raised Border with black color highlighting and white as the shadow color.

java-program-button-borders

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.