Java Tutorial/Swing/LineBorder — различия между версиями

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

Версия 17:44, 31 мая 2010

14. Creating and Setting a line Border from BorderFactory

import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;
public class Main {
  public static void main(String[] argv) {
    LineBorder lineBorder = (LineBorder)BorderFactory.createLineBorder(Color.black);
    JLabel component = new JLabel("label");
    component.setBorder(lineBorder);
  }
}





14. LineBorder Class

The line border is a single-color line of a user-defined thickness that surrounds a component.



public LineBorder(Color color)
Border lineBorder = new LineBorder (Color.RED);
public LineBorder(Color color, int thickness)
Border lineBorder = new LineBorder (Color.RED, 5);
public LineBorder (Color color, int thickness, boolean roundedCorners)
Border lineBorder = new LineBorder (Color.RED, 5, true);
public static Border createBlackLineBorder()
Border blackLine = LineBorder.createBlackLineBorder();
public static Border createGrayLineBorder()
Border grayLine = LineBorder.createGrayLineBorder();
public static Border createLineBorder(Color color)
Border lineBorder = BorderFactory.createLineBorder(Color.RED);
public static Border createLineBorder(Color color, int thickness)
Border lineBorder = BorderFactory.createLineBorder(Color.RED, 5);





14. LineBorder: color and width

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.LineBorder;
public class ALineBorder {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Line Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    Border thinBorder = LineBorder.createBlackLineBorder();
    Border thickBorder = new LineBorder(Color.WHITE, 12);
    Border roundedBorder = new LineBorder(Color.BLACK, 2, true);
    
    JButton thinButton = new JButton("1 Pixel");
    thinButton.setBorder(thinBorder);
    
    JButton thickButton = new JButton("12 Pixel");
    thickButton.setBorder(thickBorder);
    
    JButton roundedButton = new JButton("Rounded 2 Pixel");
    roundedButton.setBorder(roundedBorder);
    
    Container contentPane = frame.getContentPane();
    contentPane.add(thinButton, BorderLayout.NORTH);
    contentPane.add(thickButton, BorderLayout.CENTER);
    contentPane.add(roundedButton, BorderLayout.SOUTH);
    frame.pack();
    frame.setSize(300, frame.getHeight());
    frame.setVisible(true);
  }
}