Create a MatteBorder in Java

Description

The following code shows how to create a MatteBorder.

Example


import java.awt.Color;
import java.awt.GridLayout;
/* w  ww .  ja va2  s  .c  o  m*/
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;

public class Main extends JPanel {

  public Main() {
    super(true);
    this.setLayout(new GridLayout(1, 2, 5, 5));

    JLabel label1 = new JLabel("Matte Border");
    JLabel label2 = new JLabel("Matte Border (Icon)");

    label1.setHorizontalAlignment(JLabel.CENTER);
    label2.setHorizontalAlignment(JLabel.CENTER);

    Icon icon = new ImageIcon("a.gif");
    MatteBorder matte = new MatteBorder(35, 35, 35, 35, Color.blue);
    MatteBorder matteicon = new MatteBorder(35, 35, 35, 35, icon);
    label1.setBorder(matte);
    label2.setBorder(matteicon);

    add(label1);
    add(label2);
  }

  public static void main(String s[]) {
    JFrame frame = new JFrame("Matte Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 200);
    frame.setContentPane(new Main());
    frame.setVisible(true);
  }
}

The code above generates the following result.

Create a MatteBorder in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer