How to Create a Bevel Border in Java

This tutorial will teach you how to create a bevel border in java. A Bevel Border is an opaque border that draws a border with a three-dimensional appearance; raised or lowered. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of bevelBorder.java. 2. Import the following packages:
  1. import java.awt.*; //used to access the Color class and the GridLayout
  2. import javax.swing.*; //used to access the JFrame, JButton, and BorderFactory class
  3. import javax.swing.border.*; //used to access the BevelBorder and Border subclass of the border class library in swing
3. Initialize your variable in your Main, variable frame for JFrame, variable button1 and variable button2 as JButton.
  1.     JFrame frame = new JFrame("Bevel Border");
  2.     JButton button1 = new JButton("Raised Bevel Border");
  3.     JButton button2 = new JButton("Lowered Bevel Border");
4. First, we will create a Raised Bevel Border with the use of the Border class. We will name it as RaisedBorder and with the use of the BorderFactory class with its createBevelBorder method, we can now create a bevel border. If we declare it as Raised, the first color will be in your top, and the second will be the color in the bottom.
  1. Border RaisedBorder= BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.YELLOW, Color.RED);
Now, we will create again another Bevel Border which is lowered. We will just used the constant LOWERED in the BevelBorder. In the lowered bevel border, the first color will be in the bottom and the second one will be on the top.
  1. Border LoweredBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.GREEN, Color.CYAN);
Then, we will add the two borders(the raised and lowered bevel borders) to the two buttons.
  1.     button1.setBorder(RaisedBorder);
  2.      button2.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));
Then add the buttons into the frame using the add method. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
  1.     frame.getContentPane().add(button1);
  2.     frame.getContentPane().add(button2);
  3.     frame.setSize(300, 100);
  4.     frame.setVisible(true);
  5.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: output Here's the full code of this tutorial:
  1. import java.awt.*; //used to access the Color class and the GridLayout
  2. import javax.swing.*; //used to access the JFrame, JButton, and BorderFactory class
  3. import javax.swing.border.*; //used to access the BevelBorder and Border subclass of the border class library in swing
  4.  
  5.  
  6. public class bevelBorder {
  7.   public static void main(String args[]) {
  8.     JFrame frame = new JFrame("Bevel Border");
  9.     JButton button1 = new JButton("Raised Bevel Border");
  10.     JButton button2 = new JButton("Lowered Bevel Border");
  11.    
  12.     Border RaisedBorder= BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.YELLOW, Color.RED);
  13.     Border LoweredBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.GREEN, Color.CYAN);
  14.  
  15.     button1.setBorder(RaisedBorder);
  16.      button2.setBorder(LoweredBorder);
  17.  
  18.     frame.getContentPane().setLayout(new GridLayout(1,2));
  19.     frame.getContentPane().add(button1);
  20.     frame.getContentPane().add(button2);
  21.     frame.setSize(300, 100);
  22.     frame.setVisible(true);
  23.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24.   }
  25. }
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