How to Create an Etched Border in Java

This tutorial will teach you how to create an etched border in Java. An etched border has an etched-in or etched-out effect and in default it is in etched-in effect. Like bevel borders, it has also constants of raised and lowered etched. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of etchedBorder.java. 2. Import the following package library:
  1. import java.awt.*; //used to access the GridLayout and Color class
  2. import javax.swing.*; //used to access the JFrame and JButton class
  3. import javax.swing.border.*; //used to access the Border and EtchedBorder class
3. We will initialize variables in our Main, variable frame as JFrame and button1 labeled "Normal Etched",button2 labeled "Raised Etched", and button3 labeled "Lowered Etched" as JButton.
  1. JFrame frame = new JFrame("Etched Borders");
  2. JButton button1 = new JButton("Normal Etched");
  3. JButton button2 = new JButton("Raised Etched");
  4. JButton button3 = new JButton("Lowered Etched");
4. Now, we will create an etched border for button1 and we will use the Border class here with the EtchedBorder class instantiation. We will name the Border as normalBorder.
  1. Border normalBorder = new EtchedBorder();
  2. button1.setBorder(normalBorder);
Then we will create another etched border and will named it as raisedBorder because it will have an etched of raised, color red, and color blue. This border will set to the button2.
  1. Border raisedBorder = new EtchedBorder(EtchedBorder.RAISED, Color.RED, Color.BLUE);
  2. button2.setBorder(raisedBorder);
Lastly, we will create another etched border and will named it as loweredBorder because it will have an etched lowered, color yellow, and color green. This border will set to the button3.
  1. Border loweredBorder = new EtchedBorder(EtchedBorder.LOWERED, Color.YELLOW, Color.GREEN);
  2. button3.setBorder(loweredBorder);
5. Now, we will have its layout into Grid Layout using the setLayout method of the frame.
  1. frame.getContentPane().setLayout(new GridLayout(1, 2));
Add all the buttons to the frame using the add method.
  1. frame.getContentPane().add(button1 );
  2. frame.getContentPane().add(button2);
  3. frame.getContentPane().add(button3);
Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
  1. frame.setSize(350, 200);
  2. frame.setVisible(true);
  3. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: output Here's the full code of this tutorial:
  1. import java.awt.*; //used to access the GridLayout and Color class
  2. import javax.swing.*; //used to access the JFrame and JButton class
  3. import javax.swing.border.*; //used to access the Border and EtchedBorder class
  4.  
  5. public class etchedBorder {
  6. public static void main(String args[]) {
  7. JFrame frame = new JFrame("Etched Borders");
  8. JButton button1 = new JButton("Normal Etched");
  9. JButton button2 = new JButton("Raised Etched");
  10. JButton button3 = new JButton("Lowered Etched");
  11.  
  12. Border normalBorder = new EtchedBorder();
  13. button1.setBorder(normalBorder);
  14.  
  15. Border raisedBorder = new EtchedBorder(EtchedBorder.RAISED, Color.RED, Color.BLUE);
  16. button2.setBorder(raisedBorder);
  17.  
  18. Border loweredBorder = new EtchedBorder(EtchedBorder.LOWERED, Color.YELLOW, Color.GREEN);
  19. button3.setBorder(loweredBorder);
  20.  
  21. frame.getContentPane().setLayout(new GridLayout(1, 2));
  22. frame.getContentPane().add(button1 );
  23. frame.getContentPane().add(button2);
  24. frame.getContentPane().add(button3);
  25. frame.setSize(350, 200);
  26. frame.setVisible(true);
  27. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28. }
  29. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment