BorderLayout - Control height but not width

烂漫一生 提交于 2020-01-14 12:42:54

问题


I am relatively new to Java UI and would appreciate your help with a simple problem that I have.

I have a JPanel of type BorderLayout. The CENTER part contains a combo box.

As the outer frame is resized, the panel resizes (this is desired) and the width of the combo box changes (this is desired too). However, the height of the combobox is also changing as the panel is resized. This is not desired.

How can I ensure that my combo box expands in width but not in height?

I also have a button in LINE_START. I would like this button not to stretch at all.


回答1:


Put the combo. into the NORTH of a panel that is then added to the CENTER of the main BorderLayout. Do the same with the button.




回答2:


I would strongly suggest for you to use TableLayout my personal favourite. Since you can layout your elements as if they were in a table, without much hassle.

You can read more about it here.

This link takes you to a full site about it.

I am afraid this layout manager might be slightly outdated, certainly the examples on its page are, but it works great even in 1.7 version of Java.

Look at the example there. The option you would use to make the box to stretch (i.e. fill the available space) is TableLayout.FILL.



来源:https://stackoverflow.com/questions/10495723/borderlayout-control-height-but-not-width

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