public class GridBagFrame extends JFrame
1: // Fig. 22.29: GridBagFrame.java
2: // Demonstrating GridBagLayout.
3: import java.awt.GridBagLayout;
4: import java.awt.GridBagConstraints;
5: import java.awt.Component;
6: import javax.swing.JFrame;
7: import javax.swing.JTextArea;
8: import javax.swing.JTextField;
9: import javax.swing.JButton;
10: import javax.swing.JComboBox;
11:
12: public class GridBagFrame extends JFrame
13: {
14: private GridBagLayout layout; // layout of this frame
15: private GridBagConstraints constraints; // constraints of this layout
16:
17: // set up GUI
18: public GridBagFrame()
19: {
20: super( "GridBagLayout" );
21: layout = new GridBagLayout();
22: setLayout( layout ); // set frame layout
23: constraints = new GridBagConstraints(); // instantiate constraints
24:
25: // create GUI components
26: JTextArea textArea1 = new JTextArea( "TextArea1", 5, 10 );
27: JTextArea textArea2 = new JTextArea( "TextArea2", 2, 2 );
28:
29: String names[] = { "Iron", "Steel", "Brass" };
30: JComboBox comboBox = new JComboBox( names );
31:
32: JTextField textField = new JTextField( "TextField" );
33: JButton button1 = new JButton( "Button 1" );
34: JButton button2 = new JButton( "Button 2" );
35: JButton button3 = new JButton( "Button 3" );
36:
37: // weightx and weighty for textArea1 are both 0: the default
38: // anchor for all components is CENTER: the default
39: constraints.fill = GridBagConstraints.BOTH;
40: addComponent( textArea1, 0, 0, 1, 3 );
41:
42: // weightx and weighty for button1 are both 0: the default
43: constraints.fill = GridBagConstraints.HORIZONTAL;
44: addComponent( button1, 0, 1, 2, 1 );
45:
46: // weightx and weighty for comboBox are both 0: the default
47: // fill is HORIZONTAL
48: addComponent( comboBox, 2, 1, 2, 1 );
49:
50: // button2
51: constraints.weightx = 1000; // can grow wider
52: constraints.weighty = 1; // can grow taller
53: constraints.fill = GridBagConstraints.BOTH;
54: addComponent( button2, 1, 1, 1, 1 );
55:
56: // fill is BOTH for button3
57: constraints.weightx = 0;
58: constraints.weighty = 0;
59: addComponent( button3, 1, 2, 1, 1 );
60:
61: // weightx and weighty for textField are both 0, fill is BOTH
62: addComponent( textField, 3, 0, 2, 1 );
63:
64: // weightx and weighty for textArea2 are both 0, fill is BOTH
65: addComponent( textArea2, 3, 2, 1, 1 );
66: } // end GridBagFrame constructor
67:
68: // method to set constraints on
69: private void addComponent( Component component,
70: int row, int column, int width, int height )
71: {
72: constraints.gridx = column; // set gridx
73: constraints.gridy = row; // set gridy
74: constraints.gridwidth = width; // set gridwidth
75: constraints.gridheight = height; // set gridheight
76: layout.setConstraints( component, constraints ); // set constraints
77: add( component ); // add component
78: } // end method addComponent
79: } // end class GridBagFrame
80:
81: /**************************************************************************
82: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
83: * Pearson Education, Inc. All Rights Reserved. *
84: * *
85: * DISCLAIMER: The authors and publisher of this book have used their *
86: * best efforts in preparing the book. These efforts include the *
87: * development, research, and testing of the theories and programs *
88: * to determine their effectiveness. The authors and publisher make *
89: * no warranty of any kind, expressed or implied, with regard to these *
90: * programs or to the documentation contained in these books. The authors *
91: * and publisher shall not be liable in any event for incidental or *
92: * consequential damages in connection with, or arising out of, the *
93: * furnishing, performance, or use of these programs. *
94: *************************************************************************/