Java Reference
In-Depth Information
After the user interface has been sketched to show the relative sizes of components, the
cell position and size of each component can be determined.
The width of each component in the email interface was set to multiples of 10, making it
easy to use a grid with 10 columns.
Like grid layout, cells begin with (0,0) in the upper-left corner. The x coordinate is the
column, and the y coordinate is the row. They increase as you move to the left and down-
ward, respectively.
Figure 11.7 shows the (x,y) position and the width of each component, in cells.
0,1 (1 wide)
1,1 (9 wide)
0,0 (1 wide)
1,0 (9 wide)
FIGURE 11.7
Choosing cells for
components in the
grid.
0,2 (1 wide)
5,2 (1 wide)
1,2 (4 wide)
6,2 (4 wide)
Creating the Grid
With a well-planned sketch on graph paper, you can write the code necessary to imple-
ment the user interface.
The following statements in the email panel's constructor set it to use grid bag layout and
add a To label and text field to the panel:
public MessagePanel() {
GridBagLayout gridbag = new GridBagLayout();
setLayout(gridbag);
// add the label
JLabel toLabel = new JLabel(“To: “);
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 10;
constraints.weighty = 100;
constraints.fill = GridBagConstraints.NONE;
Search WWH ::




Custom Search