diff options
Diffstat (limited to 'lib/example/GridBagVersusTable.java')
-rw-r--r-- | lib/example/GridBagVersusTable.java | 119 |
1 files changed, 56 insertions, 63 deletions
diff --git a/lib/example/GridBagVersusTable.java b/lib/example/GridBagVersusTable.java index 53e45d6..0ed367a 100644 --- a/lib/example/GridBagVersusTable.java +++ b/lib/example/GridBagVersusTable.java | |||
@@ -1,140 +1,133 @@ | |||
1 | package example; | 1 | package example; |
2 | 2 | ||
3 | 3 | ||
4 | 4 | import java.awt.Font; | |
5 | import java.awt.*; | 5 | import java.awt.Frame; |
6 | import java.awt.event.*; | 6 | import java.awt.GridBagConstraints; |
7 | import java.awt.GridBagLayout; | ||
8 | import java.awt.event.WindowAdapter; | ||
9 | import java.awt.event.WindowEvent; | ||
10 | import java.awt.event.WindowListener; | ||
7 | import javax.swing.JButton; | 11 | import javax.swing.JButton; |
8 | import layout.TableLayout; | 12 | import layout.TableLayout; |
9 | import layout.TableLayoutConstraints; | ||
10 | 13 | ||
11 | 14 | ||
15 | public class GridBagVersusTable { | ||
12 | 16 | ||
13 | public class GridBagVersusTable | ||
14 | { | ||
15 | 17 | ||
16 | |||
17 | |||
18 | protected static void makeButton | 18 | protected static void makeButton |
19 | (Frame frame, String name, GridBagLayout gridbag, GridBagConstraints c) | 19 | (Frame frame, String name, GridBagLayout gridbag, |
20 | { | 20 | GridBagConstraints c) { |
21 | JButton button = new JButton(name); | 21 | JButton button = new JButton(name); |
22 | gridbag.setConstraints(button, c); | 22 | gridbag.setConstraints(button, c); |
23 | frame.add(button); | 23 | frame.add(button); |
24 | } | 24 | } |
25 | 25 | ||
26 | 26 | ||
27 | 27 | protected static Frame showGridBagWindow() { | |
28 | protected static Frame showGridBagWindow () | ||
29 | { | ||
30 | // Create layout and contraints object | 28 | // Create layout and contraints object |
31 | GridBagLayout gridbag = new GridBagLayout(); | 29 | GridBagLayout gridbag = new GridBagLayout(); |
32 | GridBagConstraints c = new GridBagConstraints(); | 30 | GridBagConstraints c = new GridBagConstraints(); |
33 | 31 | ||
34 | // Create frame | 32 | // Create frame |
35 | Frame frame = new Frame("GridBagLayout"); | 33 | Frame frame = new Frame("GridBagLayout"); |
36 | frame.setFont (new Font("Helvetica", Font.PLAIN, 14)); | 34 | frame.setFont(new Font("Helvetica", Font.PLAIN, 14)); |
37 | frame.setLayout (gridbag); | 35 | frame.setLayout(gridbag); |
38 | 36 | ||
39 | // Create buttons, add buttons, and apply constraints | 37 | // Create buttons, add buttons, and apply constraints |
40 | c.fill = GridBagConstraints.BOTH; | 38 | c.fill = GridBagConstraints.BOTH; |
41 | c.weightx = 1.0; | 39 | c.weightx = 1.0; |
42 | makeButton (frame, "Button1", gridbag, c); | 40 | makeButton(frame, "Button1", gridbag, c); |
43 | makeButton (frame, "Button2", gridbag, c); | 41 | makeButton(frame, "Button2", gridbag, c); |
44 | makeButton (frame, "Button3", gridbag, c); | 42 | makeButton(frame, "Button3", gridbag, c); |
45 | 43 | ||
46 | c.gridwidth = GridBagConstraints.REMAINDER; //end of row | 44 | c.gridwidth = GridBagConstraints.REMAINDER; //end of row |
47 | makeButton (frame, "Button4", gridbag, c); | 45 | makeButton(frame, "Button4", gridbag, c); |
48 | 46 | ||
49 | c.weightx = 0.0; //reset to the default | 47 | c.weightx = 0.0; //reset to the default |
50 | makeButton (frame, "Button5", gridbag, c); //another row | 48 | makeButton(frame, "Button5", gridbag, c); //another row |
51 | 49 | ||
52 | c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row | 50 | c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row |
53 | makeButton (frame, "Button6", gridbag, c); | 51 | makeButton(frame, "Button6", gridbag, c); |
54 | 52 | ||
55 | c.gridwidth = GridBagConstraints.REMAINDER; //end of row | 53 | c.gridwidth = GridBagConstraints.REMAINDER; //end of row |
56 | makeButton (frame, "Button7", gridbag, c); | 54 | makeButton(frame, "Button7", gridbag, c); |
57 | 55 | ||
58 | c.gridwidth = 1; //reset to the default | 56 | c.gridwidth = 1; //reset to the default |
59 | c.gridheight = 2; | 57 | c.gridheight = 2; |
60 | c.weighty = 1.0; | 58 | c.weighty = 1.0; |
61 | makeButton (frame, "Button8", gridbag, c); | 59 | makeButton(frame, "Button8", gridbag, c); |
62 | 60 | ||
63 | c.weighty = 0.0; //reset to the default | 61 | c.weighty = 0.0; //reset to the default |
64 | c.gridwidth = GridBagConstraints.REMAINDER; //end of row | 62 | c.gridwidth = GridBagConstraints.REMAINDER; //end of row |
65 | c.gridheight = 1; //reset to the default | 63 | c.gridheight = 1; //reset to the default |
66 | makeButton (frame, "Button9", gridbag, c); | 64 | makeButton(frame, "Button9", gridbag, c); |
67 | makeButton (frame, "Button10", gridbag, c); | 65 | makeButton(frame, "Button10", gridbag, c); |
68 | 66 | ||
69 | // Show frame | 67 | // Show frame |
70 | frame.pack(); | 68 | frame.pack(); |
71 | frame.setLocation (0, 10); | 69 | frame.setLocation(0, 10); |
72 | frame.show(); | 70 | frame.show(); |
73 | 71 | ||
74 | return frame; | 72 | return frame; |
75 | } | 73 | } |
76 | 74 | ||
77 | 75 | ||
78 | 76 | protected static Frame showTableWindow() { | |
79 | protected static Frame showTableWindow () | ||
80 | { | ||
81 | // Create frame | 77 | // Create frame |
82 | Frame frame = new Frame("TableLayout"); | 78 | Frame frame = new Frame("TableLayout"); |
83 | frame.setFont(new Font("Helvetica", Font.PLAIN, 14)); | 79 | frame.setFont(new Font("Helvetica", Font.PLAIN, 14)); |
84 | 80 | ||
85 | // Set layout | 81 | // Set layout |
86 | double f = TableLayout.FILL; | 82 | double f = TableLayout.FILL; |
87 | double p = TableLayout.PREFERRED; | 83 | double p = TableLayout.PREFERRED; |
88 | double size[][] = {{f, f, f, f}, {p, p, p, p, f}}; | 84 | double size[][] = {{f, f, f, f}, {p, p, p, p, f}}; |
89 | 85 | ||
90 | TableLayout layout = new TableLayout(size); | 86 | TableLayout layout = new TableLayout(size); |
91 | frame.setLayout (layout); | 87 | frame.setLayout(layout); |
92 | 88 | ||
93 | // Create buttons labeled Button1 to Button10 | 89 | // Create buttons labeled Button1 to Button10 |
94 | int numButton = 10; | 90 | int numButton = 10; |
95 | JButton button[] = new JButton[numButton + 1]; | 91 | JButton button[] = new JButton[numButton + 1]; |
96 | 92 | ||
97 | for (int i = 1; i <= numButton; i++) | 93 | for (int i = 1; i <= numButton; i++) { |
98 | button[i] = new JButton("Button" + i); | 94 | button[i] = new JButton("Button" + i); |
99 | 95 | } | |
96 | |||
100 | // Add buttons | 97 | // Add buttons |
101 | frame.add (button[1], "0, 0"); | 98 | frame.add(button[1], "0, 0"); |
102 | frame.add (button[2], "1, 0"); | 99 | frame.add(button[2], "1, 0"); |
103 | frame.add (button[3], "2, 0"); | 100 | frame.add(button[3], "2, 0"); |
104 | frame.add (button[4], "3, 0"); | 101 | frame.add(button[4], "3, 0"); |
105 | frame.add (button[5], "0, 1, 3, 1"); | 102 | frame.add(button[5], "0, 1, 3, 1"); |
106 | frame.add (button[6], "0, 2, 2, 2"); | 103 | frame.add(button[6], "0, 2, 2, 2"); |
107 | frame.add (button[7], "3, 2, 3, 2"); | 104 | frame.add(button[7], "3, 2, 3, 2"); |
108 | frame.add (button[8], "0, 3, 0, 4"); | 105 | frame.add(button[8], "0, 3, 0, 4"); |
109 | frame.add (button[9], "1, 3, 3, 3"); | 106 | frame.add(button[9], "1, 3, 3, 3"); |
110 | frame.add (button[10], "1, 4, 3, 4"); | 107 | frame.add(button[10], "1, 4, 3, 4"); |
111 | 108 | ||
112 | // Show frame | 109 | // Show frame |
113 | frame.pack(); | 110 | frame.pack(); |
114 | frame.setLocation (400, 10); | 111 | frame.setLocation(400, 10); |
115 | frame.show(); | 112 | frame.show(); |
116 | 113 | ||
117 | return frame; | 114 | return frame; |
118 | } | 115 | } |
119 | 116 | ||
120 | |||
121 | 117 | ||
122 | public static void main (String args[]) | 118 | public static void main(String args[]) { |
123 | { | ||
124 | WindowListener listener = | 119 | WindowListener listener = |
125 | (new WindowAdapter() | 120 | (new WindowAdapter() { |
126 | { | 121 | public void windowClosing(WindowEvent e) { |
127 | public void windowClosing (WindowEvent e) | 122 | System.exit(0); |
128 | { | ||
129 | System.exit (0); | ||
130 | } | ||
131 | } | 123 | } |
124 | } | ||
132 | ); | 125 | ); |
133 | 126 | ||
134 | Frame frame = showGridBagWindow(); | 127 | Frame frame = showGridBagWindow(); |
135 | frame.addWindowListener (listener); | 128 | frame.addWindowListener(listener); |
136 | 129 | ||
137 | frame = showTableWindow(); | 130 | frame = showTableWindow(); |
138 | frame.addWindowListener (listener); | 131 | frame.addWindowListener(listener); |
139 | } | 132 | } |
140 | } | 133 | } |