Reputation: 59
I've been tasked to make a GUI in Java by using "swing" which is an exact replica of the image below. It doesn't have to have any functionality, it's just for looks. However, there's this one part of this image I can't find or make, which is the top right corner "gender" box. How do I make that transparent-looking box that has an outline? Please help me out, and thank you!
Upvotes: 2
Views: 76
Reputation: 17454
That is called a TitledBorder
.
Example:
public class SwingTester {
public static void main(String[] args) {
createWindow();
}
private static void createWindow() {
JFrame frame = new JFrame("Swing Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createUI(frame);
frame.setSize(560, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void createUI(JFrame frame){
//Create a border
Border blackline = BorderFactory.createTitledBorder("Title");
JPanel panel = new JPanel();
LayoutManager layout = new FlowLayout();
panel.setLayout(layout);
JPanel panel1 = new JPanel();
String spaces = " ";
panel1.add(new JLabel(spaces + "Title border to JPanel" + spaces));
panel1.setBorder(blackline);
panel.add(panel1);
frame.getContentPane().add(panel, BorderLayout.CENTER);
}
}
For your case, update this line with your own title:
Border blackline = BorderFactory.createTitledBorder("Title"); //change to gender
Source: https://www.tutorialspoint.com/swingexamples/add_title_to_border_panel.htm
Upvotes: 3