TitledBorder based on LineBorder : TitiledBorder « Swing « Java Tutorial






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);
  }
}








14.101.TitiledBorder
14.101.1.Creating TitledBorder through its constructors and helper method from BorderFactory
14.101.2.Nested TitiledBorderNested TitiledBorder
14.101.3.Setting TitiledBorder DirectionSetting TitiledBorder Direction
14.101.4.Set title position
14.101.5.TitleBorder Title PositionTitleBorder Title Position
14.101.6.TitledBorder based on LineBorderTitledBorder based on LineBorder
14.101.7.TitleBorder JustificationTitleBorder Justification
14.101.8.Create a title border from another border
14.101.9.Change border text Justification alignment style
14.101.10.Customizing TitledBorder Look and Feel