Change TitledBorder Justification in Java
Description
The following code shows how to change TitleBorder Justification.
Example
//www . j av a2s . co m
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 Main {
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);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »