Why aren't my components wrapping in Swing's FlowLayout?

大兔子大兔子 提交于 2019-12-02 10:57:59

问题


Why aren't my components wrapping in this JPanel using FlowLayout? They simply run off screen and are only partially visible.

JPanel panel = new JPanel(new FlowLayout());
panel.add(new JLabel("TEST"));
// ... repeat adding JLabels until they go off screen when they SHOULD wrap
// to the next line...

That's the entirety of my code (aside from adding and packing the frame). Am I misunderstanding FlowLayout? Do I have to set some sort of size on either: the labels or the panel?


回答1:


That's because FlowLayout doesn't. I know, it sucks.

But, you could take a look at WrapLayout which is a solution to this problem




回答2:


I know this is an old question, but I stumbled upon it while trying to solve the same problem. It is not entirely true that FlowLayout will not wrap its child components.

The issue is that the JPanel with your desired FlowLayout must be inside another container that will resize and allow the FlowLayout to also resize and therefore wrap. In my experience, the CENTER region of a BorderLayout is the only such container that will allow this to happen.



来源:https://stackoverflow.com/questions/11909058/why-arent-my-components-wrapping-in-swings-flowlayout

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