Java Reference
In-Depth Information
The full program is in the files FlowLayoutJFrame.java and FlowLayoutDemo.java on
the accompanying CD. You will see a number of examples of GUIs that use the Flow-
Layout manager class later in this chapter.
extra
code on
CD
Grid Layout Managers
A GridLayout manager arranges components in a two-dimensional grid with some
number of rows and columns. With a GridLayout manager, each entry is the same
size. For example, the following says to use a GridLayout manager with aContainer ,
which can be a JFrame or other container:
GridLayout
aContainer.setLayout( new GridLayout(2, 3));
The two numbers given as arguments to the constructor GridLayout specify the num-
ber of rows and columns. This would produce the following sort of layout:
The lines will not be visible unless you do something special to make them visible.
They are just included here to show you the region boundaries.
When using a GridLayout manager, each component is stretched so that it com-
pletely fills its grid position.
Although you specify a number of rows and columns, the rules for the number of rows
and columns is more complicated than what we have said so far. If the values for the num-
ber of rows and the number of columns are both nonzero, then the number of columns
will be ignored. For example, if the specification is new GridLayout(2, 3) , then some
sample sizes are as follows: If you add six items, the grid will be as shown. If you add seven
or eight items, a fourth column is automatically added, and so forth. If you add fewer
than six components, there will be two rows and a reduced number of columns.
There is another way to specify that the number of columns is to be ignored. You can
specify that the number of columns is to be ignored by setting the number of columns to
zero, which will allow any number of columns. So a specification of (2, 0) is equivalent
to (2, 3) , and in fact is equivalent to (2, n) for any nonnegative value of n . Similarly,
you can specify that the number of rows is to be ignored by setting the number of rows
to zero, which will allow any number of rows.
When using the GridLayout class, the method add has only one argument. The
items are placed in the grid from left to right, first filling the top row, then the second
row, and so forth. You are not allowed to skip any grid position (although you will
later see that you can add something that does not show and so gives the illusion of
skipping a grid position).
A sample use of the GridLayout class is given in Display 17.9.
Search WWH ::




Custom Search