Java Swing (BoxLayout) alignment issues

血红的双手。 提交于 2019-12-02 02:30:42

问题


I am extremely new to Java Swing, and I'm having quite a bit of issues getting a nice layout going. 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:

    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


回答1:


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.

Basically you need to make sure the alignmentX value of all components is set to be left aligned.

Also:

  1. Don't use setPreferredSize() to set the size of a component. Each Swing component will determine its own preferred size.
  2. Use Java naming conventions. Variable names should NOT start with an upper case character.



回答2:


I would not recommend using setPreferredSize() AND 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.

Moreover, you might want to give your main frame a layout as well. Can you post how you used your GridBagLayout?



来源:https://stackoverflow.com/questions/23100014/java-swing-boxlayout-alignment-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!