Add titled border to JLabel in Java
Description
The following code shows how to add titled border to JLabel.
Example
/*w w w . j a va2 s .c om*/
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main extends JFrame {
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label;
label = new JLabel("Titled border");
label.setBorder(BorderFactory.createTitledBorder("Titled"));
panel.add(label);
getContentPane().add(panel);
pack();
}
public static void main(String[] args) {
Main s = new Main();
s.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »