Java Reference
In-Depth Information
JTextField bcc = new JTextField();
addComponent(bcc, 6, 2, 4, 1, 40, 100, GridBagConstraints.HORIZONTAL,
GridBagConstraints.WEST);
These four components share the same row, which makes their weightx values impor-
tant. The labels are set to 10 each, and the text fields are set to 40 each, as noted in the
initial sketch.
Listing 11.7 shows the full source code of the email panel class, MessagePanel .
LISTING 11.7
The Full Text of MessagePanel.java
1: import java.awt.*;
2: import javax.swing.*;
3:
4: public class MessagePanel extends JPanel {
5: GridBagLayout gridbag = new GridBagLayout();
6:
7: public MessagePanel() {
8: super();
9: GridBagConstraints constraints;
10: setLayout(gridbag);
11:
12: JLabel toLabel = new JLabel(“To: “);
13: JTextField to = new JTextField();
14: JLabel subjectLabel = new JLabel(“Subject: “);
15: JTextField subject = new JTextField();
16: JLabel ccLabel = new JLabel(“CC: “);
17: JTextField cc = new JTextField();
18: JLabel bccLabel = new JLabel(“BCC: “);
19: JTextField bcc = new JTextField();
20:
21: addComponent(toLabel, 0, 0, 1, 1, 10, 100,
22: GridBagConstraints.NONE, GridBagConstraints.EAST);
23: addComponent(to, 1, 0, 9, 1, 90, 100,
24: GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
25: addComponent(subjectLabel, 0, 1, 1, 1, 10, 100,
26: GridBagConstraints.NONE, GridBagConstraints.EAST);
27: addComponent(subject, 1, 1, 9, 1, 90, 100,
28: GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
29: addComponent(ccLabel, 0, 2, 1, 1, 10, 100,
30: GridBagConstraints.NONE, GridBagConstraints.EAST);
31: addComponent(cc, 1, 2, 4, 1, 40, 100,
32: GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
33: addComponent(bccLabel, 5, 2, 1, 1, 10, 100,
34: GridBagConstraints.NONE, GridBagConstraints.EAST);
35: addComponent(bcc, 6, 2, 4, 1, 40, 100,
36: GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
11
Search WWH ::




Custom Search