Java Reference
In-Depth Information
The following statements continue the preceding example, adding a button to the layout:
JButton okButton = new JButton(“OK”);
gridbag.setConstraints(okButton, constraints);
pane.add(okButton);
A constraints object must be set before the placement of each component in the grid.
Designing the Grid
Because grid bag layout is complex, it helps to do some preparatory work before using
it—either by sketching out the desired user interface on graph paper or making notes in
some other form.
Figure 11.6 shows a sketch on graph paper for the layout of a panel in an email pro-
gram's user interface.
FIGURE 11.6
Designing a user
interface on a grid.
11
The panel drawn in Figure 11.6 contains a group of labels and text fields that will be
filled out when sending a message.
A grid bag layout suits this interface because it contains components of different widths.
All the labels have the same width, but the To and Subject text fields are larger than the
CC and BCC fields. In grid bag layout, each component must have its own cell and can-
not share it with any other components. A component can take up more than one cell.
The sketch in Figure 11.6 does not indicate individual cells, but it does mark off values
from 0 to 100 to indicate the width of components. These are intended as percentage val-
ues rather than exact sizes, which is a convenient way to calculate weightx and weighty
values.
At this point, you might be wondering why there aren't percentage
values from 0 to 100 running vertically alongside the sketch. The
email interface doesn't need them—all the components will have
the same height (and, thus, the same weighty value).
NOTE
Search WWH ::




Custom Search