19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

454 Chapter 12 GUI Basics<br />

This code creates an explicit reference <strong>to</strong> the object layout of the FlowLayout class. The<br />

explicit reference is not necessary, because the object is not directly referenced in the<br />

ShowFlowLayout class.<br />

Suppose you add the same but<strong>to</strong>n <strong>to</strong> the frame ten times; will ten but<strong>to</strong>ns appear in the frame?<br />

No, a GUI <strong>com</strong>ponent such as a but<strong>to</strong>n can be added <strong>to</strong> only one container and only once in a<br />

container. Adding a but<strong>to</strong>n <strong>to</strong> a container multiple times is the same as adding it once.<br />

Note<br />

GUI <strong>com</strong>ponents cannot be shared by containers, because only one GUI <strong>com</strong>ponent can<br />

appear in only one container at a time. Therefore, the relationship between a <strong>com</strong>ponent<br />

and a container is the <strong>com</strong>position denoted by a filled diamond, as shown in Figure 12.1.<br />

Caution<br />

Do not forget <strong>to</strong> put the new opera<strong>to</strong>r before a layout manager class when setting a layout<br />

style—for example, setLayout(new FlowLayout()).<br />

Note<br />

The construc<strong>to</strong>r ShowFlowLayout() does not explicitly invoke the construc<strong>to</strong>r<br />

JFrame(), but the construc<strong>to</strong>r JFrame() is invoked implicitly. See Section 11.3.2,<br />

Construc<strong>to</strong>r Chaining.<br />

12.5.2 GridLayout<br />

The GridLayout manager arranges <strong>com</strong>ponents in a grid (matrix) formation. The <strong>com</strong>ponents<br />

are placed in the grid from left <strong>to</strong> right, starting with the first row, then the second, and so on, in<br />

the order in which they are added. The class diagram for GridLayout is shown in Figure 12.6.<br />

-rows: int<br />

-columns: int<br />

-hgap: int<br />

-vgap: int<br />

java.awt.GridLayout<br />

+GridLayout()<br />

+GridLayout(rows: int, columns: int)<br />

+GridLayout(rows: int, columns: int,<br />

hgap: int, vgap: int)<br />

The get and set methods for these data<br />

fields are provided in the class, but<br />

omitted in the UML diagram for brevity.<br />

The number of rows in the grid (default: 1).<br />

The number of columns in the grid (default: 1).<br />

The horizontal gap between the <strong>com</strong>ponents (default: 0).<br />

The vertical gap between the <strong>com</strong>ponents (default: 0).<br />

Creates a default GridLayout manager.<br />

Creates a GridLayout with a specified number of rows and columns.<br />

Creates a GridLayout manager with a specified number of rows and<br />

columns, horizontal gap, and vertical gap.<br />

FIGURE 12.6<br />

GridLayout lays out <strong>com</strong>ponents in equal-sized cells on a grid.<br />

You can specify the number of rows and columns in the grid. The basic rules are as follows:<br />

■ The number of rows or the number of columns can be zero, but not for both. If one is<br />

zero and the other is nonzero, the nonzero dimension is fixed, while the zero dimension<br />

is determined dynamically by the layout manager. For example, if you specify<br />

zero rows and three columns for a grid that has ten <strong>com</strong>ponents, GridLayout creates<br />

three fixed columns of four rows, with the last row containing one <strong>com</strong>ponent. If<br />

you specify three rows and zero columns for a grid that has ten <strong>com</strong>ponents,<br />

GridLayout creates three fixed rows of four columns, with the last row containing<br />

two <strong>com</strong>ponents.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!