MatteBorder is a class which is used to make a matte-like border of either a solid color or a tiled icon. It is a part of javax.swing.border package and contains different styles of Borders for Components. This class helps us to set an icon or a solid color as a border and the insets of the border can also be applied using this class.
Constructor of the class are:
- MatteBorder(Icon tileIcon):Creates a matte border with the specified tile icon.
- MatteBorder(Insets borderInsets, Color matteColor):Creates a matte border with the specified insets and color.
- MatteBorder(Insets borderInsets, Icon tileIcon):Creates a matte border with the specified insets and tile icon.
- MatteBorder(int top, int left, int bottom, int right, Color matteColor):Creates a matte border with the specified insets and color.
- MatteBorder(int top, int left, int bottom, int right, Icon tileIcon):Creates a matte border with the specified insets and tile icon.
Commonly used methods are:
method |
explanation |
getBorderInsets() |
Returns the insets of the border. |
getBorderInsets(Component c, Insets insets) |
Reinitialize the insets parameter with this Border’s current Insets. |
getMatteColor() |
Returns the color used for tiling the border or null if a tile icon is being used. |
getTileIcon() |
returns the title icon for the border |
isBorderOpaque() |
returns whether border is opaque or not |
Below programs illustrate the MatteBorder class:
- Program to apply matte border using solid colors: We will create a frame f titled ”frame” and will create a panel which will act as a container. We will create two labels l1 and l. We will set the border of both the frame to the matte border using setborder() function one label will have a red border and other will have blue. We will add the label to the panel and panel to the frame. We will set the size of the frame to 400,400 using setSize(400,400) and display the frame using show().
Java
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class matte1 extends JFrame {
static JFrame f;
public static void main(String[] args)
{
f = new JFrame( "frame" );
matte1 s = new matte1();
JPanel p = new JPanel();
JLabel l = new JLabel( " this is a matte border 2" );
JLabel l1 = new JLabel( " this is a matte border 1" );
l.setBorder( new MatteBorder( 4 , 4 , 4 , 4 , Color.red));
l1.setBorder( new MatteBorder( 4 , 4 , 4 , 4 , Color.blue));
p.add(l1);
p.add(l);
f.add(p);
f.setSize( 400 , 400 );
f.show();
}
}
|
- Output:
- Program to apply matte border using icons : We will create a frame f titled ” frame” and will create a panel which will act as a container. We will create two labels l1 and l. We will set the border of both the frame to the matte border using setborder() function one label will have an image icon as border and other will have another image icon. We will import the images using new ImageIcon() function. We will add the label to the panel and panel to the frame. We will set the size of the frame to 400,400 using setSize(400,400) and display the frame using show().
Java
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class matte extends JFrame {
static JFrame f;
public static void main(String[] args)
{
f = new JFrame( "frame" );
matte s = new matte();
JPanel p = new JPanel();
JLabel l = new JLabel( " this is a matte border 2" );
JLabel l1 = new JLabel( " this is a matte border 1" );
l.setBorder( new MatteBorder( new ImageIcon( "f:\\gfg.png" )));
l1.setBorder( new MatteBorder( new ImageIcon( "f:\\gfg.jpg" )));
p.add(l1);
p.add(l);
f.add(p);
f.setSize( 400 , 400 );
f.show();
}
}
|
- Output:
- Program to apply matte border using solid color and image by specifying insets: We will create a frame f titled ” frame” and will create a panel which will act as a container. We will create two labels l1 and l. We will set the border of both the frame to the matte border using setborder() function one label will have an image icon as border and other will have another image icon. We will import the images using new ImageIcon() function. We will specify the insets or width of the border using new Insets() function. We will add the label to the panel and panel to the frame. We will set the size of the frame to 400,400 using setSize(400,400) and display the frame using show().
Java
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class matte3 extends JFrame {
static JFrame f;
public static void main(String[] args)
{
f = new JFrame( "frame" );
matte3 s = new matte3();
JPanel p = new JPanel();
JLabel l = new JLabel( " this is a matte border 2" );
JLabel l1 = new JLabel( " this is a matte border 1" );
l.setBorder( new MatteBorder( new Insets( 4 , 7 , 4 , 10 ), Color.red));
l1.setBorder( new MatteBorder( new Insets( 10 , 4 , 10 , 4 ), new ImageIcon( "f:\\gfg.png" )));
p.add(l1);
p.add(l);
f.add(p);
f.setSize( 400 , 400 );
f.show();
}
}
|
- Output:
Last Updated :
16 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...