Java Swing | BevelBorder and SoftBevelBorder
BevelBorder and SoftBevelBorder are a part of javax.swing.Border package. This package contains different Border for Components. BevelBorder is an implementation of a simple two line bevel border. Bevel Border and Soft Bevel Border are almost same but Soft Bevel Border has softened corners.
Constructor for the class BevelBorder:
- BevelBorder(int Type): Creates a bevel border with the specified type and whose colors will be derived from the background color of the component passed into the paintBorder method.
- BevelBorder(int Type, Color h, Color s):Creates a bevel border with the specified type, highlight and shadow colors.
- BevelBorder(int Type, Color highlightOuterColor, Color highlightInnerColor, Color shadowOuterColor, Color shadowInnerColor):Creates a bevel border with the specified type, highlight and shadow colors.
Constructor for the class SoftBevelBorder:
- SoftBevelBorder(int Type): Creates a bevel border with the specified type and whose colors will be derived from the background color of the component passed into the paintBorder method.
- SoftBevelBorder(int Type, Color h, Color s): Creates a bevel border with the specified type, highlight and shadow colors.
- SoftBevelBorder(int Type, Color highlightOuterColor, Color highlightInnerColor, Color shadowOuterColor, Color shadowInnerColor): Creates a bevel border with the specified type, highlight and shadow colors.
Commonly used methods are:
method | explanation |
---|---|
getBevelType() | returns the type of bevel border |
getBorderInsets(Component c, Insets insets) | Reinitialize the insets parameter with this Border’s current Insets. |
getHighlightInnerColor() | Returns the inner highlight color of the bevel border. |
getHighlightInnerColor(Component c) | Returns the inner highlight color of the bevel border when rendered on the specified component. |
getHighlightOuterColor() | Returns the outer highlight color of the bevel border. |
getHighlightOuterColor(Component c) | Returns the outer highlight color of the bevel border when rendered on the specified component. |
getShadowInnerColor() | Returns the inner shadow color of the bevel border. |
getShadowInnerColor(Component c) | Returns the inner shadow color of the bevel border when rendered on the specified component. |
getShadowOuterColor() | Returns the outer shadow color of the bevel border. |
getShadowOuterColor(Component c) | Returns the outer shadow color of the bevel border when rendered on the specified component. |
isBorderOpaque() | returns whether border is opaque or not |
Below programs illustrate the Bevel Border class:
- Program to create a simple bevel border with specified type: To create a bevel border, we first create a JPanel object p, all the borders will be applied to this object. The JPanel will be hosted inside the JFrame f, which is the outermost container in this program. To set the bevel border, we create 2 JLabel objects, “l” and “l1”, one for the raised type border and the other for lowered type border. The borders are applied by the function l.setBorder() and l1.setBorder(). Finally, the borders are added to the JPanel by p.add() function and the results are shown by f.show().
Java
// Java Program to create a simple bevel // border with specified type import java.awt.event.*; import java.awt.*; import javax.swing.*; import javax.swing.border.*; class bevel extends JFrame { // frame static JFrame f; // main class public static void main(String[] args) { // create a new frame f = new JFrame( "frame" ); // create a object bevel s = new bevel(); // create a panel JPanel p = new JPanel(); // create a label JLabel l = new JLabel( " this is bevel border of raised type" ); // create a label JLabel l1 = new JLabel( " this is bevel border of lowered type" ); // set border for panel l.setBorder( new BevelBorder(BevelBorder.RAISED)); // set border for label l1.setBorder( new BevelBorder(BevelBorder.LOWERED)); // add button to panel p.add(l1); p.add(l); f.add(p); // set the size of frame f.setSize( 400 , 400 ); f.show(); } } |
- Output:
- Program to apply bevel border with specified colors to highlight and shadow: To create a bevel border with highlighted colors, we first create a JPanel object p, all the borders will be applied to this object. The JPanel will be hosted inside the JFrame f, which is the outermost container in this program. To set the bevel border, we create 2 JLabel objects, “l” and “l1”, one for the raised type border and the other for lowered type border. The borders are applied by the function l.setBorder() and l1.setBorder(). And the colors are passed into these constructors as parameters, for ex: Color.red etc.) Finally, the borders are added to the JPanel by p.add() function and the results are shown by f.show().
Java
// java Program to apply bevel border with // specified colors to highlight and shadow import java.awt.event.*; import java.awt.*; import javax.swing.*; import javax.swing.border.*; class bevel1 extends JFrame { // frame static JFrame f; // main class public static void main(String[] args) { // create a new frame f = new JFrame( "frame" ); // create a object bevel1 s = new bevel1(); // create a panel JPanel p = new JPanel(); // create a label JLabel l = new JLabel( " this is bevel border of raised type" ); // create a label JLabel l1 = new JLabel( " this is bevel border of lowered type" ); // set border for panel l.setBorder( new BevelBorder(BevelBorder.RAISED, Color.red, Color.blue)); // set border for label l1.setBorder( new BevelBorder(BevelBorder.LOWERED, Color.black, Color.red, Color.pink, Color.yellow)); // add button to panel p.add(l1); p.add(l); f.add(p); // set the size of frame f.setSize( 400 , 400 ); f.show(); } } |
- Output:
Below programs illustrate the SoftBevelBorder class:
- Program to create a simple Soft bevel border with specified type: To create a soft bevel border, we first create a JPanel object p, all the borders will be applied to this object. The JPanel will be hosted inside the JFrame f. To set the bevel border, we create 2 JLabel objects, “l” and “l1”. The borders are applied by the function l.setBorder() and l1.setBorder(). To make the border soft, we call the constructor in the parameter of setBorder() method, which is indicated by line “new SoftBevelBorder()). Finally, the borders are added to the JPanel by p.add() function and the results are shown by f.show().
Java
// java Program to create a simple Soft bevel border // with specified type import java.awt.event.*; import java.awt.*; import javax.swing.*; import javax.swing.border.*; class bevel2 extends JFrame { // frame static JFrame f; // main class public static void main(String[] args) { // create a new frame f = new JFrame( "frame" ); // create a object bevel2 s = new bevel2(); // create a panel JPanel p = new JPanel(); // create a label JLabel l = new JLabel( " this is bevel border of raised type" ); // create a label JLabel l1 = new JLabel( " this is bevel border of lowered type" ); // set border for panel l.setBorder( new SoftBevelBorder(BevelBorder.RAISED)); // set border for label l1.setBorder( new SoftBevelBorder(BevelBorder.LOWERED)); // add button to panel p.add(l1); p.add(l); f.add(p); // set the size of frame f.setSize( 400 , 400 ); f.show(); } } |
- Output:
- Program to apply soft bevel border with specified colors to highlight and shadow: To create a soft bevel border, we first create a JPanel object p, all the borders will be applied to this object. The JPanel will be hosted inside the JFrame f. To set the bevel border, we create 2 JLabel objects, “l” and “l1”. The borders are applied by the function l.setBorder() and l1.setBorder(). To make the border soft, we call the constructor in the parameter of setBorder() method, which is indicated by line “new SoftBevelBorder()). And the colors are passed into these constructors as parameters, for ex: Color.red etc.). Finally, the borders are added to the JPanel by p.add() function and the results are shown by f.show().
Java
// Java Program to apply soft bevel border with // specified colors to highlight and shadow import java.awt.event.*; import java.awt.*; import javax.swing.*; import javax.swing.border.*; class bevel3 extends JFrame { // frame static JFrame f; // main class public static void main(String[] args) { // create a new frame f = new JFrame( "frame" ); // create a object bevel3 s = new bevel3(); // create a panel JPanel p = new JPanel(); // create a label JLabel l = new JLabel( " this is bevel border of raised type" ); // create a label JLabel l1 = new JLabel( " this is bevel border of lowered type" ); // set border for panel l.setBorder( new SoftBevelBorder(BevelBorder.RAISED, Color.red, Color.blue)); // set border for label l1.setBorder( new SoftBevelBorder(BevelBorder.LOWERED, Color.black, Color.red, Color.pink, Color.yellow)); // add button to panel p.add(l1); p.add(l); f.add(p); // set the size of frame f.setSize( 400 , 400 ); f.show(); } } |
- Output:
Note: The above programs might not run in an online IDE please use an offline compile.
Reference:
- https://docs.oracle.com/javase/7/docs/api/javax/swing/border/BevelBorder.html
- https://docs.oracle.com/javase/7/docs/api/javax/swing/border/SoftBevelBorder.html
Please Login to comment...