简体   繁体   English

Java Swing (BoxLayout) 对齐问题

[英]Java Swing (BoxLayout) alignment issues

I am extremely new to Java Swing, and I'm having quite a bit of issues getting a nice layout going.我对 Java Swing 非常陌生,并且在获得良好布局方面遇到了很多问题。 I have checked out google, and even other answers on this website, but no information I find seems to solve the issue.我已经查看了谷歌,甚至本网站上的其他答案,但我发现没有任何信息似乎可以解决问题。 Here is the result of my efforts:这是我努力的结果:

在此处输入图片说明

As you can see, the label, text field, and button are all out of alignment.如您所见,标签、文本字段和按钮都没有对齐。 It is my goal for all of them to have the same left-hand border, and for the button and text field to have the same right-hand border, with these left and right hand borders being each the same distance from the left and righthand sides of my window.我的目标是让它们都具有相同的左侧边框,并且按钮和文本字段具有相同的右侧边框,这些左侧​​和右侧边框与左侧和右侧的距离相同我窗户的两侧。

Here are the important parts of my code:以下是我的代码的重要部分:

    public void run()
    {
         JFrame frame = new JFrame("Arduino Server");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         InstancePanel = new ServerGUIPanel();
         frame.getContentPane().add(InstancePanel);
         frame.pack();
         frame.setVisible(true);
    }

And, in ServerGUIPanel.java:并且,在 ServerGUIPanel.java 中:

    public ServerGUIPanel()
    {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setPreferredSize(new Dimension(500, 500));
        setBorder(new EmptyBorder(10, 10, 10, 10));



        StatusLabel = new JLabel("STATUS: BOOTUP");
        add(StatusLabel);

        PortField = new JTextField();
        PortField.setPreferredSize(new Dimension(5000, 20));
        PortField.setMaximumSize(PortField.getPreferredSize());
        PortField.setActionCommand("PortChanged");
        add(PortField);

        ConnectionButton = new JButton();
        ConnectionButton.setPreferredSize(new Dimension(5000, 20));
        ConnectionButton.setMaximumSize(ConnectionButton.getPreferredSize());
        ConnectionButton.setActionCommand("ConnectionClicked");
        add(ConnectionButton);
    }

Does anyone have a simple solution to this?有没有人对此有一个简单的解决方案? What am I doing wrong here?我在这里做错了什么?

Thank you very much!非常感谢!

--Georges Oates Larsen ——乔治·奥茨·拉森

Read the section from the Swing tutorial on How to Use BoxLayout for the basics of using a BoxLayout as well as a section on alignment issues.阅读 Swing 教程中关于 如何使用 BoxLayout的部分,了解 使用 BoxLayout的基础知识以及关于对齐问题的部分。

Basically you need to make sure the alignmentX value of all components is set to be left aligned.基本上,您需要确保所有组件的alignmentX 值都设置为左对齐。

Also:还:

  1. Don't use setPreferredSize() to set the size of a component.不要使用 setPreferredSize() 来设置组件的大小。 Each Swing component will determine its own preferred size.每个 Swing 组件将确定其自己的首选大小。
  2. Use Java naming conventions.使用 Java 命名约定。 Variable names should NOT start with an upper case character.变量名不应以大写字符开头。

I would not recommend using setPreferredSize() AND setMaximumSize().我不建议使用 setPreferredSize() 和 setMaximumSize()。 The latter will cause problems when stretching your main frame.后者在拉伸主框架时会导致问题。 [Your components will likely not want resize] [您的组件可能不想调整大小]

You should be using layout managers to handle all the alignments itself.您应该使用布局管理器来处理所有对齐本身。 I would stay away from using BoxLayout in this case, as different components want to size differently, and that will sway the alignment when added into your BoxLayout panel.在这种情况下,我会避免使用 BoxLayout,因为不同的组件想要不同的大小,并且在添加到 BoxLayout 面板时会影响对齐方式。

Moreover, you might want to give your main frame a layout as well.此外,您可能还想为您的主框架提供一个布局。 Can you post how you used your GridBagLayout?你能发布你如何使用你的 GridBagLayout 吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM