質問

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?

役に立ちましたか?

解決

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

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top