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

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

Текущая версия на 15:32, 31 мая 2010

14. CompoundBorder: to combine borders before associating them with a component.

A compound border is composed of two border objects. You can nest an inside border within the insets of an outside border.



public CompoundBorder()
public static CompoundBorder createCompoundBorder()

public CompoundBorder(Border outside, Border inside)
Border compoundBorder = new CompoundBorder(lineBorder, matteBorder);

public static CompoundBorder createCompoundBorder(Border outside, Border inside)
Border compoundBorder = BorderFactory.createCompoundBorder(lineBorder, matteBorder);





14. CompoundBorder with double line borders and Lowered Bevel Border

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.rupoundBorder;
import javax.swing.border.LineBorder;
public class CompoundBorderLabel extends JFrame {
  JLabel label = new JLabel("A label with some borders");
  public CompoundBorderLabel() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border border1 = new CompoundBorder(LineBorder.createBlackLineBorder(),
        BorderFactory.createLineBorder(Color.red, 20));
    Border border2 = new CompoundBorder(border1, BorderFactory
        .createLoweredBevelBorder());
    label.setBorder(border2);
    getContentPane().add(label, BorderLayout.CENTER);
    pack();
  }
  public static void main(String arg[]) {
    CompoundBorderLabel m = new CompoundBorderLabel();
    m.setVisible(true);
  }
}





14. Creating CompoundBorder with LineBorder

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.rupoundBorder;
import javax.swing.border.LineBorder;
public class ACompoundBorder {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Compound Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border lineBorder = LineBorder.createBlackLineBorder();
    Border bevelBorder = BorderFactory.createRaisedBevelBorder();
    Border bevelLineBorder = new CompoundBorder(bevelBorder, lineBorder);
    JButton bevelLineButton = new JButton("Bevel Line");
    bevelLineButton.setBorder(bevelLineBorder);
    Border redBorder = BorderFactory.createLineBorder(Color.MAGENTA, 2);
    Border orangeBorder = BorderFactory.createLineBorder(Color.BLUE, 2);
    Border yellowBorder = BorderFactory.createLineBorder(Color.YELLOW, 5);
    Border greenBorder = BorderFactory.createLineBorder(Color.GREEN, 2);
    Border blueBorder = BorderFactory.createLineBorder(Color.ORANGE, 4);
    Border magentaBorder = BorderFactory.createLineBorder(Color.RED, 3);
    Border twoColorBorder = new CompoundBorder(magentaBorder, blueBorder);
    Border threeColorBorder = new CompoundBorder(twoColorBorder, greenBorder);
    Border fourColorBorder = new CompoundBorder(threeColorBorder, yellowBorder);
    Border fiveColorBorder = new CompoundBorder(fourColorBorder, orangeBorder);
    Border sixColorBorder = new CompoundBorder(fiveColorBorder, redBorder);
    JButton rainbowButton = new JButton("Rainbow");
    rainbowButton.setBorder(sixColorBorder);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(1, 2));
    contentPane.add(bevelLineButton);
    contentPane.add(rainbowButton);
    frame.setSize(300, 100);
    frame.setVisible(true);
  }
}