Java Tutorial/Swing/MatteBorder

Материал из Java эксперт
Перейти к: навигация, поиск

14. Color MatteBorder

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;
public class ColorMatteBorder {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Color Matted Border");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border solidBorder = new MatteBorder(10, 5, 2, 20, Color.RED);
    JButton solidButton = new JButton("10x5x2x20");
    solidButton.setBorder(solidBorder);
    Container contentPane = frame.getContentPane();
    contentPane.add(solidButton, BorderLayout.CENTER);
    frame.setSize(300, 100);
    frame.setVisible(true);
  }
}





14. Creating and Setting a MatteBorder from BorderFactory

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.border.MatteBorder;
public class Main {
  public static void main(String[] argv) {
    ImageIcon icon = new ImageIcon("image.gif");
    MatteBorder matteBorder = (MatteBorder)BorderFactory.createMatteBorder(-1, -1, -1, -1, icon);
    JLabel component = new JLabel("label");
    component.setBorder(matteBorder);
  }
}





14. MatteBorder

  1. A matte border displays a matte pattern.
  2. The matte pattern is created by using either a solid color or an icon.
  3. Solid matte borders are created by using the specified color.
  4. Tiled matte borders are created by using an icon that displays a matte picture.

The following constructors create a matte border:



public MatteBorder(int top, int left, int bottom, int right, Color color)   
  public MatteBorder(int top, int left, int bottom, int right, Icon tileIcon)   
  public MatteBorder(Icon tileIcon)





14. Using Icon to create border: MatteBorder

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;
public class MatteBorderSample {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Sample Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Icon diamondIcon = new ImageIcon("yourImage.gif");
    Border matteBorder = new MatteBorder(5, 10, 5, 10, diamondIcon);
    
    JLabel aLabel = new JLabel("Bevel");
    aLabel.setBorder(matteBorder);
    aLabel.setHorizontalAlignment(JLabel.CENTER);
    frame.add(aLabel);
    frame.setSize(400, 200);
    frame.setVisible(true);
  }
}