Every swing component that is a subclass of JComponent can have a border. A border is a decorative element that virtually group components by drawing a line around them. By default, a component does not have a border.
import javax.swing.*;
import javax.swing.border.*;
class JPanelExample extends JFrame
{
JPanelExample()
{
JPanel p1 = new JPanel();
JLabel lblRoll = new JLabel("Rollno");
JLabel lblName = new JLabel("Name");
JTextField txtRoll = new JTextField(15);
JTextField txtName = new JTextField(15);
Border b = BorderFactory.createTitledBorder("Student Information");
p1.setBorder(b);
p1.add(lblRoll);
p1.add(txtRoll);
p1.add(lblName);
p1.add(txtName);
add(p1);
}
}
class JavaSwingBorderExample
{
public static void main(String args[])
{
JPanelExample frame = new JPanelExample();
frame.setTitle("Java Swing Border Example");
frame.setBounds(200,250,250,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}