Java Reference
In-Depth Information
FlowLayout
FlowLayout is the default, or preset, layout manager for Panels and Applets.
If you do not program a layout manager, Java uses FlowLayout, which treats a
container as a set of rows. As components are added to the container,
FlowLayout places them in rows, from left to right. The height of the item
placed in the row determines the height of that entire row. The number of rows
is determined by the size of the container. If the component cannot fit on the
current row, FlowLayout moves it to the next row. If components do not fill the
row, the components are centered in the row.
If you decide to change back to FlowLayout from a different layout manager,
the layout manager must be constructed with the setLayout() method using the
following:
setLayout(new FlowLayout());
If you want to change FlowLayout's alignment, you can use code such as the
following code:
setLayout(new FlowLayout(FlowLayout.RIGHT));
to specify one of three different alignment constants as an argument or parame-
ter to the constructor for FlowLayout: LEFT, RIGHT, or CENTER. In Java, any
constant , such as LEFT, RIGHT, or CENTER, is entered in capital letters.
Once FlowLayout has been specified, components can be added to the
container with the usual add() method. For example, the following code:
add(custNameLabel);
adds a Label component with the name custNameLabel. Figure 5-22 displays
an application with five Button components added to the container using the
default CENTER location of the FlowLayout layout manager.
components added
using FlowLayout
layout manager
Button
components
centered
FIGURE 5-22
 
Search WWH ::




Custom Search