// Fig. 11.21: GridBagDemo2.java // Demonstrating GridBagLayout constants. import java.applet.Applet; import java.awt.*; public class GridBagDemo2 extends Applet { private Choice cb; private TextField tf; private List m; private Button b1, b2, b3, b4, b5; private GridBagLayout gbLayout; private GridBagConstraints gbConstraints; public void init() { gbLayout = new GridBagLayout(); setLayout( gbLayout ); // applet // instantiate gridbag constraints gbConstraints = new GridBagConstraints(); // create some components cb = new Choice( ); cb.add( "Pine" ); cb.add( "Ash" ); cb.add( "Pecan" ); tf = new TextField( "TextField" ); m = new List( 3, false ); m.add( "Java" ); b1 = new Button( "null" ); b2 = new Button( "eins" ); b3 = new Button( "zwei" ); b4 = new Button( "drei" ); b5 = new Button( "vier" ); // textfield gbConstraints.weightx = 1; gbConstraints.weighty = 1; gbConstraints.fill = GridBagConstraints.BOTH; gbConstraints.gridwidth = GridBagConstraints.REMAINDER; addComponent( tf ); // button b1 // weightx and weighty are 1: fill is BOTH gbConstraints.gridwidth = 1; addComponent( b1 ); // button b2 // weightx and weighty are 1: fill is BOTH gbConstraints.gridwidth = GridBagConstraints.RELATIVE; addComponent( b2 ); // button b3 // weightx and weighty are 1: fill is BOTH gbConstraints.gridwidth = GridBagConstraints.REMAINDER; addComponent( b3 ); // choicebox // weightx is 1: fill is BOTH gbConstraints.weighty = 0; gbConstraints.gridwidth = GridBagConstraints.REMAINDER; addComponent( cb ); // button b4 // weightx is 1: fill is BOTH gbConstraints.weighty = 1; gbConstraints.gridwidth = GridBagConstraints.REMAINDER; addComponent( b4 ); // button b5 // weightx and weighty are 1: fill is BOTH gbConstraints.gridwidth = GridBagConstraints.RELATIVE; addComponent( b5 ); // list // weightx and weighty are 1: fill is BOTH gbConstraints.gridwidth = GridBagConstraints.REMAINDER; addComponent( m ); } // addComponent is programmer-defined private void addComponent( Component c ) { gbLayout.setConstraints( c, gbConstraints ); add( c ); // add component to applet } }