public class GridBagFrame2 extends JFrame
1: // Fig. 22.31: GridBagFrame2.java
2: // Demonstrating GridBagLayout constants.
3: import java.awt.GridBagLayout;
4: import java.awt.GridBagConstraints;
5: import java.awt.Component;
6: import javax.swing.JFrame;
7: import javax.swing.JComboBox;
8: import javax.swing.JTextField;
9: import javax.swing.JList;
10: import javax.swing.JButton;
11:
12: public class GridBagFrame2 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 GridBagFrame2()
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: String metals[] = { "Copper", "Aluminum", "Silver" };
27: JComboBox comboBox = new JComboBox( metals );
28:
29: JTextField textField = new JTextField( "TextField" );
30:
31: String fonts[] = { "Serif", "Monospaced" };
32: JList list = new JList( fonts );
33:
34: String names[] = { "zero", "one", "two", "three", "four" };
35: JButton buttons[] = new JButton[ names.length ];
36:
37: for ( int count = 0; count < buttons.length; count++ )
38: buttons[ count ] = new JButton( names[ count ] );
39:
40: // define GUI component constraints for textField
41: constraints.weightx = 1;
42: constraints.weighty = 1;
43: constraints.fill = GridBagConstraints.BOTH;
44: constraints.gridwidth = GridBagConstraints.REMAINDER;
45: addComponent( textField );
46:
47: // buttons[0] -- weightx and weighty are 1: fill is BOTH
48: constraints.gridwidth = 1;
49: addComponent( buttons[ 0 ] );
50:
51: // buttons[1] -- weightx and weighty are 1: fill is BOTH
52: constraints.gridwidth = GridBagConstraints.RELATIVE;
53: addComponent( buttons[ 1 ] );
54:
55: // buttons[2] -- weightx and weighty are 1: fill is BOTH
56: constraints.gridwidth = GridBagConstraints.REMAINDER;
57: addComponent( buttons[ 2 ] );
58:
59: // comboBox -- weightx is 1: fill is BOTH
60: constraints.weighty = 0;
61: constraints.gridwidth = GridBagConstraints.REMAINDER;
62: addComponent( comboBox );
63:
64: // buttons[3] -- weightx is 1: fill is BOTH
65: constraints.weighty = 1;
66: constraints.gridwidth = GridBagConstraints.REMAINDER;
67: addComponent( buttons[ 3 ] );
68:
69: // buttons[4] -- weightx and weighty are 1: fill is BOTH
70: constraints.gridwidth = GridBagConstraints.RELATIVE;
71: addComponent( buttons[ 4 ] );
72:
73: // list -- weightx and weighty are 1: fill is BOTH
74: constraints.gridwidth = GridBagConstraints.REMAINDER;
75: addComponent( list );
76: } // end GridBagFrame2 constructor
77:
78: // add a component to the container
79: private void addComponent( Component component )
80: {
81: layout.setConstraints( component, constraints );
82: add( component ); // add component
83: } // end method addComponent
84: } // end class GridBagFrame2
85:
86: /**************************************************************************
87: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
88: * Pearson Education, Inc. All Rights Reserved. *
89: * *
90: * DISCLAIMER: The authors and publisher of this book have used their *
91: * best efforts in preparing the book. These efforts include the *
92: * development, research, and testing of the theories and programs *
93: * to determine their effectiveness. The authors and publisher make *
94: * no warranty of any kind, expressed or implied, with regard to these *
95: * programs or to the documentation contained in these books. The authors *
96: * and publisher shall not be liable in any event for incidental or *
97: * consequential damages in connection with, or arising out of, the *
98: * furnishing, performance, or use of these programs. *
99: *************************************************************************/