Java Swing | BevelBorder and SoftBevelBorder
Last Updated :
16 Apr, 2021
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
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class bevel extends JFrame {
static JFrame f;
public static void main(String[] args)
{
f = new JFrame( "frame" );
bevel s = new bevel();
JPanel p = new JPanel();
JLabel l = new JLabel( " this is bevel border of raised type" );
JLabel l1 = new JLabel( " this is bevel border of lowered type" );
l.setBorder( new BevelBorder(BevelBorder.RAISED));
l1.setBorder( new BevelBorder(BevelBorder.LOWERED));
p.add(l1);
p.add(l);
f.add(p);
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
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class bevel1 extends JFrame {
static JFrame f;
public static void main(String[] args)
{
f = new JFrame( "frame" );
bevel1 s = new bevel1();
JPanel p = new JPanel();
JLabel l = new JLabel( " this is bevel border of raised type" );
JLabel l1 = new JLabel( " this is bevel border of lowered type" );
l.setBorder( new BevelBorder(BevelBorder.RAISED, Color.red,
Color.blue));
l1.setBorder( new BevelBorder(BevelBorder.LOWERED, Color.black,
Color.red, Color.pink, Color.yellow));
p.add(l1);
p.add(l);
f.add(p);
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
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class bevel2 extends JFrame {
static JFrame f;
public static void main(String[] args)
{
f = new JFrame( "frame" );
bevel2 s = new bevel2();
JPanel p = new JPanel();
JLabel l = new JLabel( " this is bevel border of raised type" );
JLabel l1 = new JLabel( " this is bevel border of lowered type" );
l.setBorder( new SoftBevelBorder(BevelBorder.RAISED));
l1.setBorder( new SoftBevelBorder(BevelBorder.LOWERED));
p.add(l1);
p.add(l);
f.add(p);
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
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class bevel3 extends JFrame {
static JFrame f;
public static void main(String[] args)
{
f = new JFrame( "frame" );
bevel3 s = new bevel3();
JPanel p = new JPanel();
JLabel l = new JLabel( " this is bevel border of raised type" );
JLabel l1 = new JLabel( " this is bevel border of lowered type" );
l.setBorder( new SoftBevelBorder(BevelBorder.RAISED, Color.red,
Color.blue));
l1.setBorder( new SoftBevelBorder(BevelBorder.LOWERED, Color.black,
Color.red, Color.pink, Color.yellow));
p.add(l1);
p.add(l);
f.add(p);
f.setSize( 400 , 400 );
f.show();
}
}
|
- Output:
Note: The above programs might not run in an online IDE please use an offline compile.
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...