Java Reference
In-Depth Information
LISTING 11.7
Continued
37: }
38:
39: private void addComponent(Component component, int gridx, int gridy,
40: int gridwidth, int gridheight, int weightx, int weighty, int fill,
41: int anchor) {
42:
43: GridBagConstraints constraints = new GridBagConstraints();
44: constraints.gridx = gridx;
45: constraints.gridy = gridy;
46: constraints.gridwidth = gridwidth;
47: constraints.gridheight = gridheight;
48: constraints.weightx = weightx;
49: constraints.weighty = weighty;
50: constraints.fill = fill;
51: constraints.anchor = anchor;
52: gridbag.setConstraints(component, constraints);
53: add(component);
54: }
55: }
After the panel has been compiled, it can be used in any graphical user interface (pre-
sumably this panel would be incorporated into an email program's interface for writing
messages).
Figure 11.8 shows how it looks when added to a simple frame 320 pixels wide by 120
pixels tall.
FIGURE 11.8
Viewing the panel
in an application's
user interface.
Because the panel does not stipulate its own size, the frame's dimensions determine the
height and width of the panel. This fluidity demonstrates a strength of Swing's grid and
grid bag layouts—they enable components to adapt to the space available to them in an
interface.
 
Search WWH ::




Custom Search