英文:
How do I make this GUI?
问题
我被委托使用 Java 中的 "swing" 制作一个图形用户界面(GUI),它是下面图片的精确复制品。它不需要具备任何功能,只是为了外观。然而,有一个部分在这个图片中我找不到或无法制作,那就是右上角的 "gender" 方框。我该如何制作那个具有透明外观且带有轮廓的方框?请帮我解决这个问题,谢谢!
英文:
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!
答案1
得分: 3
这被称为 TitledBorder
。
示例:
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){
//创建边框
Border blackline = BorderFactory.createTitledBorder("标题");
JPanel panel = new JPanel();
LayoutManager layout = new FlowLayout();
panel.setLayout(layout);
JPanel panel1 = new JPanel();
String spaces = " ";
panel1.add(new JLabel(spaces + "向 JPanel 添加标题边框" + spaces));
panel1.setBorder(blackline);
panel.add(panel1);
frame.getContentPane().add(panel, BorderLayout.CENTER);
}
}
对于您的情况,请使用您自己的标题更新此行:
Border blackline = BorderFactory.createTitledBorder("标题"); // 更改为性别
来源:https://www.tutorialspoint.com/swingexamples/add_title_to_border_panel.htm
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论