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

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

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

14. Change border text Justification alignment style

import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class Main {
  public static void main(String[] argv) {
    LineBorder border = new LineBorder(Color.red);
    TitledBorder titledBorder = BorderFactory.createTitledBorder(border, "Title");
    
    // Or: DEFAULT_JUSTIFICATION, LEFT, RIGHT
    titledBorder.setTitleJustification(TitledBorder.CENTER);
  }
}





14. Create a title border from another border

import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class Main {
  public static void main(String[] argv) {
    LineBorder border = new LineBorder(Color.red);
    TitledBorder titledBorder = BorderFactory.createTitledBorder(border, "Title");
  }
}





14. Creating TitledBorder through its constructors and helper method from BorderFactory

  1. A titled border is a combination of a title string and any of the borders.
  2. You control the position of the title with a justification.
  3. You can also specify the font and color of the title.
  4. To create a titled border, you need to specify the title and the border.
  5. If no border is specified, an etched border is used by default.



public TitledBorder(Border border)
Border titledBorder = new TitledBorder(lineBorder);
public static TitledBorder createTitledBorder(Border border)
Border titledBorder = BorderFactory.createTitledBorder(lineBorder);

public TitledBorder(String title)
Border titledBorder = new TitledBorder("Hello");
public static TitledBorder createTitledBorder(String title)
Border titledBorder = BorderFactory.createTitledBorder("Hello");

public TitledBorder(Border border, String title)
Border titledBorder = new TitledBorder(lineBorder, "Hello");
public static TitledBorder createTitledBorder(Border border, String title)
Border titledBorder = BorderFactory.createTitledBorder(lineBorder, "Hello");

public TitledBorder(Border border, String title, int justification, int position)
Border titledBorder = new TitledBorder(lineBorder, "Hello", TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM);
public static TitledBorder createTitledBorder(Border border, String title, int justification, int position)
Border titledBorder = BorderFactory.createTitledBorder(lineBorder, "Hello", TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM);

public TitledBorder(Border border, String title, int justification, int position, Font font)
Font font = new Font("Serif", Font.ITALIC, 12);
Border titledBorder = new TitledBorder(lineBorder, "Hello", TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM, font);
public static TitledBorder createTitledBorder(Border border, String title, int justification, int position, Font font)
Font font = new Font("Serif", Font.ITALIC, 12);
Border titledBorder = BorderFactory.createTitledBorder(lineBorder, "Hello", TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM, font);
public TitledBorder(Border border, String title, int justification, int position, Font font, Color color)
Font font = new Font("Serif", Font.ITALIC, 12);
Border titledBorder = new TitledBorder(lineBorder, "Hello", TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM, font, Color.RED);
public static TitledBorder createTitledBorder(Border border, String title, int justification, int position, Font font, Color color)
Font font = new Font("Serif", Font.ITALIC, 12);
Border titledBorder = BorderFactory.createTitledBorder(lineBorder, "Hello", TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM, font, Color.RED);





14. Customizing TitledBorder Look and Feel

Property StringObject TypeTitledBorder.fontFontTitledBorder.titleColorColorTitledBorder.borderBorder


14. Nested TitiledBorder

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.TitledBorder;
public class DoubleTitle {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Double Title");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    TitledBorder topBorder = BorderFactory.createTitledBorder("Top");
    topBorder.setTitlePosition(TitledBorder.TOP);
    
    TitledBorder doubleBorder = new TitledBorder(topBorder, "Bottom", TitledBorder.RIGHT,
        TitledBorder.BOTTOM);
    
    JButton doubleButton = new JButton();
    doubleButton.setBorder(doubleBorder);
    
    Container contentPane = frame.getContentPane();
    contentPane.add(doubleButton, BorderLayout.CENTER);
    frame.setSize(300, 100);
    frame.setVisible(true);
  }
}





14. Setting TitiledBorder Direction

import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class ChangingTitleBorderDirection {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Sample Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border lineBorder = new LineBorder(Color.RED, 5);
    
    Font font = new Font("Serif", Font.ITALIC, 12);
    Border titledBorder = new TitledBorder(lineBorder, "Hello", TitledBorder.LEFT,
        TitledBorder.BELOW_BOTTOM, font, Color.RED);
    
    JLabel aLabel = new JLabel("Bevel");
    aLabel.setBorder(titledBorder);
    aLabel.setHorizontalAlignment(JLabel.CENTER);
    frame.add(aLabel);
    frame.setSize(400, 200);
    frame.setVisible(true);
  }
}





14. Set title position

import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class Main {
  public static void main(String[] argv) {
    LineBorder border = new LineBorder(Color.red);
    TitledBorder titledBorder = BorderFactory.createTitledBorder(border, "Title");
    
    // Or: DEFAULT_POSITION, ABOVE_TOP, TOP, ABOVE_BOTTOM, BOTTOM, BELOW_BOTTOM
    titledBorder.setTitlePosition(TitledBorder.BELOW_TOP);
  }
}





14. TitleBorder Justification

import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.TitledBorder;
public class TitledJustBorder {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Justified Titled Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    TitledBorder leftBorder = BorderFactory.createTitledBorder("Left");
    leftBorder.setTitleJustification(TitledBorder.LEFT);
    
    JButton leftButton = new JButton();
    leftButton.setBorder(leftBorder);
    
    TitledBorder rightBorder = BorderFactory.createTitledBorder("Right");
    rightBorder.setTitleJustification(TitledBorder.RIGHT);
    
    JButton rightButton = new JButton();
    rightButton.setBorder(rightBorder);
    
    TitledBorder centerBorder = BorderFactory.createTitledBorder("Center");
    centerBorder.setTitleJustification(TitledBorder.CENTER);
    
    JButton centerButton = new JButton();
    centerButton.setBorder(centerBorder);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(3, 1));
    contentPane.add(leftButton);
    contentPane.add(rightButton);
    contentPane.add(centerButton);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





14. TitleBorder Title Position

import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.TitledBorder;
public class TitledPostBorder {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Positioned Titled Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    TitledBorder aboveTopBorder = BorderFactory.createTitledBorder("AboveTop");
    aboveTopBorder.setTitlePosition(TitledBorder.ABOVE_TOP);
    JButton aboveTopButton = new JButton();
    aboveTopButton.setBorder(aboveTopBorder);
    
    TitledBorder topBorder = BorderFactory.createTitledBorder("Top");
    topBorder.setTitlePosition(TitledBorder.TOP);
    JButton topButton = new JButton();
    topButton.setBorder(topBorder);
    
    TitledBorder belowTopBorder = BorderFactory.createTitledBorder("BelowTop");
    belowTopBorder.setTitlePosition(TitledBorder.BELOW_TOP);
    JButton belowTopButton = new JButton();
    belowTopButton.setBorder(belowTopBorder);
    
    TitledBorder aboveBottomBorder = BorderFactory.createTitledBorder("AboveBottom");
    aboveBottomBorder.setTitlePosition(TitledBorder.ABOVE_BOTTOM);
    JButton aboveBottomButton = new JButton();
    aboveBottomButton.setBorder(aboveBottomBorder);
    
    TitledBorder bottomBorder = BorderFactory.createTitledBorder("Bottom");
    bottomBorder.setTitlePosition(TitledBorder.BOTTOM);
    JButton bottomButton = new JButton();
    bottomButton.setBorder(bottomBorder);
    
    TitledBorder belowBottomBorder = BorderFactory.createTitledBorder("BelowBottom");
    belowBottomBorder.setTitlePosition(TitledBorder.BELOW_BOTTOM);
    JButton belowBottomButton = new JButton();
    belowBottomButton.setBorder(belowBottomBorder);
    
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(3, 2));
    contentPane.add(aboveTopButton);
    contentPane.add(aboveBottomButton);
    contentPane.add(topButton);
    contentPane.add(bottomButton);
    contentPane.add(belowTopButton);
    contentPane.add(belowBottomButton);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





14. TitledBorder based on LineBorder

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
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.LineBorder;
import javax.swing.border.TitledBorder;
public class ATitledBorder {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Titled Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    Border thisBorder = BorderFactory.createTitledBorder("Easy");
    
    Border thatBorder1 = new LineBorder(Color.RED);
    Border thatBorder2 = new TitledBorder(thatBorder1, "Harder");
    
    Font font = new Font("Serif", Font.ITALIC, 12);
    Border thatBorder = new TitledBorder(thatBorder2, "Harder", TitledBorder.LEFT,
        TitledBorder.ABOVE_BOTTOM, font, Color.RED);
    
    JButton thisButton = new JButton("Easy");
    thisButton.setBorder(thisBorder);
    
    JButton thatButton = new JButton("Harder");
    thatButton.setBorder(thatBorder);
    
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(1, 2));
    contentPane.add(thisButton);
    contentPane.add(thatButton);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}