Java Tutorial/Swing/Custom Border

Материал из Java эксперт
Версия от 04:57, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

14. Creating a Compound Border

import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class Main {
  public static void main(String[] argv) {
    LineBorder border1 = new LineBorder(Color.red);
    TitledBorder border2 = new TitledBorder("asdf");
    Border newBorder = BorderFactory.createCompoundBorder(border1, border2);
    JButton component = new JButton("button");
    component.setBorder(newBorder);
  }
}





14. Creating and Setting a Lowered BevelBorder from BorderFactory

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;
public class Main {
  public static void main(String[] argv) {
    BevelBorder loweredBevelBorder = (BevelBorder)BorderFactory.createLoweredBevelBorder();
    JLabel component = new JLabel("label");
    component.setBorder(loweredBevelBorder);
  }
}





14. Creating and Setting a Raised BevelBorder from BorderFactory

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;
public class Main {
  public static void main(String[] argv) {
    
    BevelBorder raisedBevelBorder = (BevelBorder)BorderFactory.createRaisedBevelBorder();
    JLabel component = new JLabel("label");
    component.setBorder(raisedBevelBorder);
  }
}





14. Creating Your Own Borders

  1. implements the Border interface
  2. extend the AbstractBorder class

AbstractBorder class is the better, since certain Swing classes has optimizations for the AbstractBorder-specific methods.


14. implements Border interface

import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
class SimpleBorder implements Border {  
  int top;  
  int left;   
  int bottom;   
  int right;  
  Color color = null;   
  public SimpleBorder() {  
      this.top = 2;   
      this.left = 4;   
      this.bottom = 8;   
      this.right = 10;   
      this.color = Color.RED;   
  }   
  public void paintBorder(Component c, 
                          Graphics g,  
                          int x, int y,
                          int width, int height) {

      Insets insets = getBorderInsets(c);   
      if (color != null)  
          g.setColor(color);  

      g.fill3DRect(0, 
                   0, 
                   width-insets.right, 
                   insets.top, 
                   true); 
      g.fill3DRect(0, insets.top, insets.left,  
                      height-insets.top, true);   
      g.fill3DRect(insets.left, height-insets.bottom,   
                   width-insets.left, insets.bottom, true);   
      g.fill3DRect(width-insets.right, 0, insets.right,   
                   height-insets.bottom, true);   
  }   
  public Insets getBorderInsets(Component c) {  
      return new Insets(top, left, bottom, right);  
  }   
  public boolean isBorderOpaque() {   
      return true;  
  }   
}
public class CustomBorderDemo {
  public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Aaaaaaaaaaa");
    button.setBorder(new SimpleBorder());
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
  }
 
}





14. Subclass AbstractBorder

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
public class RedGreenBorder extends AbstractBorder {
  public boolean isBorderOpaque() {
    return true;
  }
  public Insets getBorderInsets(Component c) {
    return new Insets(3, 3, 3, 3);
  }
  public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    Insets insets = getBorderInsets(c);
    Color horizontalColor;
    if (c.isEnabled()) {
      boolean pressed = false;
      if (c instanceof AbstractButton) {
        ButtonModel model = ((AbstractButton) c).getModel();
        pressed = model.isPressed();
      }
      if (pressed) {
        horizontalColor = Color.RED;
      } else {
        horizontalColor = Color.GREEN;
      }
    } else {
      horizontalColor = Color.LIGHT_GRAY;
    }
    g.setColor(horizontalColor);
    g.fillRect(0, 0, width, insets.top);
  }
  public static void main(String args[]) {
    JFrame frame = new JFrame("My Border");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border border = new RedGreenBorder();
    JButton helloButton = new JButton("Hello");
    helloButton.setBorder(border);
    JButton braveButton = new JButton("Brave New");
    braveButton.setBorder(border);
    braveButton.setEnabled(false);
    JButton worldButton = new JButton("World");
    worldButton.setBorder(border);
    Container contentPane = frame.getContentPane();
    contentPane.add(helloButton, BorderLayout.NORTH);
    contentPane.add(braveButton, BorderLayout.CENTER);
    contentPane.add(worldButton, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
  }
}