问题
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