So I have some code like this inside init() for an applet:

layout = new BorderLayout();
setLayout(layout);

northPanel = new JPanel(new FlowLayout());

northPanel.add(inputDropDown);
northPanel.add(lowBoundLabel);
northPanel.add(lowBoundField);
northPanel.add(highBoundLabel);
northPanel.add(highBoundField);
northPanel.add(new JLabel("using"));
northPanel.add(categoriesField);
northPanel.add(new JLabel("categories"));
northPanel.add(showTotalsBox);
northPanel.add(refreshButton);

add(northPanel, BorderLayout.NORTH);

Now when I test it, all of the elements are in a straight line and do not wrap around when there is not enough space. I even made sure to specify that the panel is FlowLayout (even though it's the default) and it didn't change anything.

Shouldn't they wrap instead of just going off the screen? What's going on? I came up with a temporary solution by changing the northPanel to BorderLayout, splitting up these elements into to separate panels and adding them to North and South. However, the elements just disappear off the screen without the necessary space in this method so I'd rather have them wrap around.

有帮助吗?

解决方案

This is actually exactly how FlowLayout works, annoying isn't it...

Take a look at WrapLayout instead...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top