Java Swing (BoxLayout) alignment issues

被刻印的时光 ゝ 提交于 2019-12-02 02:57:28

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.

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?

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