FlowLayout
adds components in raster order:
left to right, top to bottom.
BoxLayout
Sometimes you want components to be lined up in a strictly
vertical (or strictly horizontal) order.
This can be done with a BoxLayout
layout manager.
Here is a constructor for it:
BoxLayout(Container contain, int axis) contain: the container this layout manager is for axis: BoxLayout.X_AXIS — for left to right alignment BoxLayout.Y_AXIS — for top to bottom alignment
Notice that the constructor needs a reference to the container that it is manageing.
To set the layout manager of a JPanel
to BoxLayout
with vertical alignment,
do this:
JPanel panel = new JPanel(); panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS) );
Here is another example GUI. Look at it and decide where a vertical layout has been used
Where has BoxLayout
with vertical alignment been used?