Java Reference
In-Depth Information
Here the second row is clearly centered. Each button component has been set to its preferred size,
which comfortably accommodates the text for the label. The centering is determined by the alignment
constraint for the layout manager, which defaults to CENTER .
It can also be set to RIGHT or LEFT by using a different constructor. For example, you could have
created the layout manager with the statement:
FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
The flow layout manager then left-aligns each row of components in the container. If you run the
program with this definition and resize the window, it will look like:
Now the buttons are left aligned. Two of the buttons have spilled from the first row to the second
because there is insufficient space across the width of the window to accommodate them all.
The flow layout manager in the previous examples applies a default gap of 5 pixels between
components in a row, and between one row and the next. You can choose values for the horizontal and
vertical gaps by using yet another FlowLayout constructor. You can set the horizontal gap to 20 pixels
and the vertical gap to 30 pixels with the statement:
FlowLayout flow = new FlowLayout(FlowLayout.LEFT, 20, 30);
If you run the program with this definition of the layout manager, when you resize the window you will
see the components distributed with the spacing specified.
Search WWH ::




Custom Search