Java Swing (BoxLayout) alignment issues

后端 未结 2 404
夕颜
夕颜 2021-01-22 11:33

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 in

相关标签:
2条回答
  • 2021-01-22 12:10

    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?

    0 讨论(0)
  • 2021-01-22 12:19

    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.
    0 讨论(0)
提交回复
热议问题