/Users/lyon/j4p/src/gui/goslab/GOSLAB.java

1    package gui.goslab; 
2     
3    import java.awt.*; 
4    import java.awt.event.ActionEvent; 
5    import java.awt.event.ActionListener; 
6     
7    public class GOSLAB 
8            extends Frame 
9            implements ActionListener { 
10       String labels[] = {"red", "green", "blue", "moo"}; 
11       LabelControlPanel lcp = new LabelControlPanel(labels); 
12       ScrollbarPanel sbp = new ScrollbarPanel(labels.length); 
13       Panel panels[] = {sbp, lcp}; 
14    
15       ButtonControlPanel bcp = new ButtonControlPanel(this); 
16    
17       static GridBagConstraintFrame gbcf = 
18               new GridBagConstraintFrame(); 
19    
20       public GOSLAB( 
21               GridBagConstraints gbcsb, 
22               GridBagConstraints gbclp, 
23               GridBagConstraints gbcbcp) { 
24    
25           GridBagLayout gbl = new GridBagLayout(); 
26           setLayout(gbl); 
27           gbl.setConstraints(sbp, gbcsb); 
28           gbl.setConstraints(lcp, gbclp); 
29           gbl.setConstraints(bcp, gbcbcp); 
30           add(sbp); 
31           add(lcp); 
32           add(bcp); 
33           setSize(200, 200); 
34           pack(); 
35           setVisible(true); 
36       } 
37    
38       public GOSLAB() { 
39           gbcf.setVisible(true); 
40    
41           GridBagLayout ma = new GridBagLayout(); 
42           GridBagConstraints gbc = new GridBagConstraints(); 
43           gbc.gridx = 0; 
44           gbc.gridy = 0; 
45           gbc.fill = GridBagConstraints.HORIZONTAL; 
46           gbc.weightx = 200; 
47           gbc.weighty = 200; 
48           setLayout(ma); 
49           ma.setConstraints(sbp, gbc); 
50    
51           add(sbp); 
52           gbc.gridx = GridBagConstraints.RELATIVE; 
53           gbc.gridwidth = GridBagConstraints.REMAINDER; 
54           add(lcp); 
55    
56           gbc.gridy = GridBagConstraints.BOTH; 
57           gbc.weightx = 100; 
58    
59           ma.setConstraints(bcp, gbc); 
60           add(bcp); 
61           setSize(200, 200); 
62           pack(); 
63           setVisible(true); 
64    
65       } 
66    
67       public static void main( 
68               String args[]) { 
69           new GOSLAB(); 
70       } 
71    
72       public void actionPerformed(ActionEvent e) { 
73           Object o = e.getSource(); 
74           if (o == bcp.cancelButton) { 
75               setVisible(false); 
76               return; 
77           } 
78           if (o == bcp.okButton) { 
79               sbp.printValues(); 
80               return; 
81           } 
82       } 
83   } 
84    
85   class LabelControlPanel 
86           extends Panel { 
87       Label lba[]; 
88    
89       LabelControlPanel( 
90               String labels[]) { 
91           setLayout(new GridLayout(0, 1)); 
92           lba = 
93                   new Label[labels.length]; 
94           for (int i = 0; 
95                i < labels.length; i++) { 
96               lba[i] = new Label( 
97                       labels[i]); 
98               add(lba[i]); 
99           } 
100      } 
101  } 
102   
103  class ScrollbarPanel 
104          extends Panel { 
105      Scrollbar sba[]; 
106   
107      public int[] getValues() { 
108          int values[] = new int[sba.length]; 
109          for (int i = 0; 
110               i < sba.length; 
111               i++) 
112              values[i] = 
113                      sba[i].getValue(); 
114   
115          return values; 
116      } 
117   
118      public void printValues() { 
119          int values[] = getValues(); 
120          for (int i = 0; i < values.length; i++) 
121              System.out.println(values[i]); 
122      } 
123   
124      ScrollbarPanel(int N) { 
125          sba = 
126                  new Scrollbar[N]; 
127          GridBagLayout sbp = new GridBagLayout(); 
128          GridBagConstraints sb = new GridBagConstraints(); 
129   
130          setLayout(sbp); 
131          sb.gridx = 0; 
132          sb.fill = GridBagConstraints.HORIZONTAL; 
133          sb.weightx = 200; 
134          sb.weighty = 200; 
135   
136          int j = 20; 
137          for (int i = 0; i < sba.length; i++) { 
138              sba[i] = 
139                      new Scrollbar( 
140                              Scrollbar.HORIZONTAL); 
141              sba[i].setValue(50); 
142              sbp.setConstraints(sba[i], sb); 
143              add(sba[i]); 
144              sb.gridy = GridBagConstraints.RELATIVE; 
145              sb.fill = GridBagConstraints.HORIZONTAL; 
146              sb.weightx = 100; 
147              sb.weighty = 100; 
148   
149          } 
150      } 
151  } 
152   
153  class ButtonControlPanel extends 
154          Panel { 
155      Button okButton = new Button("ok"); 
156      Button cancelButton = new Button("cancel"); 
157   
158      ButtonControlPanel( 
159              ActionListener al) { 
160          setLayout(new FlowLayout()); 
161          add(okButton); 
162          add(cancelButton); 
163          okButton.addActionListener(al); 
164          cancelButton.addActionListener(al); 
165      } 
166  } 
167   
168  class GridBagConstraintPanel 
169          extends Panel { 
170      TextField gxtf = new TextField("0"); 
171      TextField gytf = new TextField("0"); 
172      TextField gwtf = new TextField("1"); 
173      TextField ghtf = new TextField("1"); 
174      TextField gwxtf = new TextField("200"); 
175      TextField gwytf = new TextField("200"); 
176      TextField ipadxtf = new TextField("0"); 
177      TextField ipadytf = new TextField("0"); 
178      Choice fillChoice = new 
179              Choice(); 
180      GridBagConstraints gbc = new GridBagConstraints(); 
181   
182      GridBagConstraintPanel() { 
183          setLayout(new GridLayout(0, 2)); 
184          add(new Label("gridx=")); 
185          add(gxtf); 
186          add(new Label("gridy=")); 
187          add(gytf); 
188          add(new Label("gridHeight=")); 
189          add(ghtf); 
190          add(new Label("gridWidth=")); 
191          add(gwtf); 
192          add(new Label("weightx=")); 
193          add(gwxtf); 
194          add(new Label("weighty=")); 
195          add(gwytf); 
196          add(new Label("ipadx=")); 
197          add(ipadxtf); 
198          add(new Label("ipady=")); 
199          add(ipadytf); 
200   
201          fillChoice.add("NONE"); 
202          fillChoice.add("VERTICAL"); 
203          fillChoice.add("HORIZONTAL"); 
204          fillChoice.add("BOTH"); 
205          add(new Label("fillChoice=")); 
206          add(fillChoice); 
207      } 
208   
209      GridBagConstraints getGridBagConstraints() { 
210          gbc.gridx = Integer.parseInt(gxtf.getText()); 
211          gbc.gridy = Integer.parseInt(gytf.getText()); 
212          gbc.gridwidth = Integer.parseInt(gwtf.getText()); 
213          gbc.gridheight = Integer.parseInt(ghtf.getText()); 
214          String fs = fillChoice.getSelectedItem(); 
215          if (fs.equals("NONE")) 
216              gbc.fill = GridBagConstraints.NONE; 
217          if (fs.equals("VERTICAL")) 
218              gbc.fill = GridBagConstraints.VERTICAL; 
219          if (fs.equals("HORIZONTAL")) 
220              gbc.fill = GridBagConstraints.HORIZONTAL; 
221          if (fs.equals("BOTH")) 
222              gbc.fill = GridBagConstraints.BOTH; 
223          gbc.weightx = Integer.parseInt(gwxtf.getText()); 
224          gbc.weighty = Integer.parseInt(gwytf.getText()); 
225          gbc.ipadx = Integer.parseInt(ipadxtf.getText()); 
226          gbc.ipady = Integer.parseInt(ipadytf.getText()); 
227          return gbc; 
228      } 
229  } 
230   
231  class GridBagConstraintFrame extends 
232          Frame implements ActionListener { 
233      ButtonControlPanel bcp 
234              = new ButtonControlPanel(this); 
235      GridBagConstraintPanel gbcpsb 
236              = new GridBagConstraintPanel(); 
237      GridBagConstraintPanel gbcplp 
238              = new GridBagConstraintPanel(); 
239      GridBagConstraintPanel gbcpbp 
240              = new GridBagConstraintPanel(); 
241   
242      GridBagConstraintFrame() { 
243          super("Scrollbar,Label & button Constraints"); 
244          setLayout(new GridLayout(2, 0)); 
245          add(gbcpsb); 
246          add(gbcplp); 
247          add(gbcpbp); 
248          add(bcp); 
249          pack(); 
250          show(); 
251      } 
252   
253      public void actionPerformed(ActionEvent e) { 
254          Object target = e.getSource(); 
255          if (target == bcp.okButton) { 
256              GOSLAB gl = new GOSLAB( 
257                      gbcpsb.getGridBagConstraints(), 
258                      gbcplp.getGridBagConstraints(), 
259                      gbcpbp.getGridBagConstraints()); 
260              gl.pack(); 
261              gl.show(); 
262   
263          } 
264      } 
265  } 
266   
267   
268