Programming and Problem Solving with Java

(やまだぃちぅ) #1
8.2 Formatting Output | 383

Figure 8.2 A Grid Layout with Five Rows and Two Columns


is the same size as every other column, and the rows are also equal in size. Figure 8.2 shows
a frame with a 5 2 grid.
When we specifyGridLayoutas the manager for the content pane, we provide the con-
structor with a pair of integer arguments that determine the number of rows and columns
in the pane. If one of the arguments is zero, then that dimension isn’t specified and the grid
grows as needed in that direction to accommodate the contents of the pane. The first ar-
gument is the number of rows and the second argument is the number of columns.
For example, the layout in Figure 8.2 is specified with a method call to setLayout.


datePane.setLayout(new GridLayout(5,2)); // 5 rows and 2 columns


If we wanted thedatePanecontent pane to have two columns and an arbitrary number of
rows, we would write the call with the first argument to theGridLayoutconstructor being zero:


datePane.setLayout(new GridLayout(0,2)); // Any number of rows of 2 columns


Using zero for the number of columns (the second argument) would allow GridLayoutto
partition the pane into any number of columns. The number it chooses depends on the size
of the largest label (as all columns are equal in size) and the number of these labels that can
fit horizontally on the screen. Because this setup could lead to some rather strange column
configurations, it is more common for the user to specify the number of columns.GridLayout

Free download pdf