如何制作这个GUI?

huangapple go评论76阅读模式
英文:

How do I make this GUI?

问题

我被委托使用 Java 中的 "swing" 制作一个图形用户界面(GUI),它是下面图片的精确复制品。它不需要具备任何功能,只是为了外观。然而,有一个部分在这个图片中我找不到或无法制作,那就是右上角的 "gender" 方框。我该如何制作那个具有透明外观且带有轮廓的方框?请帮我解决这个问题,谢谢!

图片链接:
如何制作这个GUI?

英文:

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!

如何制作这个GUI?

答案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);    
   }
}

如何制作这个GUI?

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

huangapple
  • 本文由 发表于 2020年9月29日 03:12:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64108241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定