开发者

add()ing to a JPanel breaks its size

I have a JPanel identified by myPanel. I create a series of JButtons, and add() them to myPanel. At the end of my generating-and-adding loop, I call myPanel.validate(). The buttons show up.

The problem is the size of the panel is affected. No matter what Layout Manager I choose, the buttons are always added on 开发者_如何学Cthe same line (even though there is more space beneath them).

I have tried setting myPanel's maximum size and setting its size after every add(). No matter what I do, after that validate(), the panel is blown up and my application's GUI is screwed.

Any ideas?


I create a series of JButtons, and add() them to myPanel... the buttons are always added on the same line (even though there is more space beneath them).

None of the default layout managers provide automatic wrapping. You need to specify how you want wrapping to occur. A GridLayout or a GridBagLayout can be used in these cases.

Or you can try the Wrap Layout which was written for this purpose.


i would firstly set a preferred size on the jpanel:

myPanel.setPreferredSize(new Dimension(X, Y));

this will tell your layout manager how to try to fit the contents within this dimension.

without seeing your source code, i can't add much more than that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜