Swing layout - Using a grid while keeping component dimensions

不打扰是莪最后的温柔 提交于 2019-12-24 06:28:03

问题


I'd like to make a login bar for an application and I can't figure out how to organize a series of JLabels and JTextFields such that they are organized in a horizontal grid without these same components being resized to fit each cell. I also want to make sure that the group of components isn't resized below a certain width. How can this be achieved?

Edit: Thanks for the answers everyone. I'll have a look at MigLayout and SpringLayout later. Due to time constraints I'm going to have to make do with Visual Editor and use a null layout. The component placement and dimensions have to be adjusted by hand but at least they stay put. Here's a picture showing what I wanted to do.

bar http://img145.imageshack.us/img145/7356/bargw.png


回答1:


Use MigLayout as your layout manager, it's extremely flexible, and supports what you're asking quite easily. You can set size constraints. If you need any further help, post some example code using Swing and MigLayout which shows what you're trying to do, and then I'll advise you on how to do what you want to achieve.

You probably want some additional cells which 'grow' to fill the remaining space. This can be achieved with column constraints, by inserting 'push' between the columns (specified by [..]) to expand the gap. You don't need any placeholder components in this case. (i.e., [pref!]10px[40px::]push[pref!]10px[40px::])




回答2:


You have to use different layout. FlowLayout or BoxLayout will work in your case, but I would suggest MigLayout simply because it will cover all your needs and replace all others .




回答3:


Check out the section from the Swing tutorial on Using Layout Managers.

The SpringLayout has an example that does exaclty this.

The GridBagLayout is more difficult to use but also support row/column type layout.

Finally, you can still use a GridLayout. Just add the text fields to a JPanel first, then the panel will grow but the text field won't.



来源:https://stackoverflow.com/questions/2889300/swing-layout-using-a-grid-while-keeping-component-dimensions

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