Java Reference
In-Depth Information
This code creates an explicit reference to the object layout of the FlowLayout class. The
explicit reference is not necessary, because the object is not directly referenced in the
ShowFlowLayout class.
Suppose you add the same button to the frame ten times; will ten buttons appear in the frame?
No, a GUI component such as a button can be added to only one container and only once in a
container. Adding a button to a container multiple times is the same as adding it once.
Note
GUI components cannot be shared by containers, because only one GUI component can
appear in only one container at a time. Therefore, the relationship between a component
and a container is the composition denoted by a filled diamond, as shown in Figure 12.1.
Caution
Do not forget to put the new operator before a layout manager class when setting a lay-
out style—for example, setLayout(new FlowLayout()) .
Note
The constructor ShowFlowLayout() does not explicitly invoke the constructor
JFrame() , but the constructor JFrame() is invoked implicitly. See Section 11.3.2,
Constructor Chaining.
12.5.2 GridLayout
The GridLayout manager arranges components in a grid (matrix) formation. The components
are placed in the grid from left to right, starting with the first row, then the second, and so on, in
the order in which they are added. The class diagram for GridLayout is shown in Figure 12.6.
The get and set methods for these data
fields are provided in the class, but
omitted in the UML diagram for brevity.
java.awt.GridLayout
-rows: int
-columns: int
-hgap: int
-vgap: int
The number of rows in the grid (default: 1).
The number of columns in the grid (default: 1).
The horizontal gap between the components (default: 0).
The vertical gap between the components (default: 0).
+GridLayout()
+GridLayout(rows: int, columns: int)
+GridLayout(rows: int, columns: int,
hgap: int, vgap: int)
Creates a default GridLayout manager.
Creates a GridLayout with a specified number of rows and columns.
Creates a GridLayout manager with a specified number of rows and
columns, horizontal gap, and vertical gap.
F IGURE 12.6
GridLayout lays out components in equal-sized cells on a grid.
You can specify the number of rows and columns in the grid. The basic rules are as follows:
The number of rows or the number of columns can be zero, but not for both. If one is
zero and the other is nonzero, the nonzero dimension is fixed, while the zero dimen-
sion is determined dynamically by the layout manager. For example, if you specify
zero rows and three columns for a grid that has ten components, GridLayout cre-
ates three fixed columns of four rows, with the last row containing one component. If
you specify three rows and zero columns for a grid that has ten components,
GridLayout creates three fixed rows of four columns, with the last row containing
two components.
 
Search WWH ::




Custom Search