The FlowLayout
just places component one beside the other in a left-to-right order. When the width reaches the one of the container that has that layout it simply wraps on the other line.
If you want to arrange them in a grid-style layout (like it seems you want) you can use the GridLayout
that allows you to specify the number of columns and rows:
component.setLayout(new GridLayout(2,2))
The only downside of GridLayout
is that every cell of the grid will be of the same size (which is usually good if you just have JButtons
or JLabels
but when you mix things it will be visually bad).
If you really need more power go with the GridBagLayout
, very customizable but with a steeper learning curve at the beginning.
Probably your size problem is related to the fact that you are using setSize
but in Swing these things have strange behaviours, you should try by setting setPreferredSize(200,200)
instead of setSize
. But don't ask me why!
NOTE: you should ALWAYS refer to the frame's content pane and not to the frame it self. When you set layout you should do getContentPane().setLayout(..)
, when you add items you should do getContentPane().add(..)
and so on.
Errata: now every JFrame
add
, remove
, setLayout
automatically forward to the content pane.