//GBL2.java //Improve the GridbagLayout //Andy Harris, 05/00 import java.awt.*; import java.applet.*; import java.awt.event.*; public class GBL2 extends Applet{ Frame frmGBL = new Frame("Grid Bag Demo"); Button btnShowFrame = new Button("Show Frame"); EventHandler evt = new EventHandler(); GridBagLayout gbl = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); public void init(){ add (btnShowFrame); btnShowFrame.addActionListener(evt); setUpFrame(); } // end init private class EventHandler extends WindowAdapter implements ActionListener{ public void actionPerformed(ActionEvent e){ frmGBL.setVisible(true); frmGBL.setSize(400,300); } // end actionPerformed public void windowClosing(WindowEvent e){ frmGBL.setVisible(false); frmGBL.dispose(); } // end windowClosing } // end eventHandler class public void setUpFrame(){ Button btn1 = new Button("Component 1"); Button btn2 = new Button("Component 2"); Button btn3 = new Button("Component 3"); Button btn4 = new Button("Component 4"); Button btn5 = new Button("Component 5"); Button btn6 = new Button("Component 6"); Button btn7 = new Button("Component 7"); frmGBL.setLayout(gbl); frmGBL.addWindowListener(evt); // comp x y ht wd x% y% fill placeComponent(btn1, 0, 0, 1, 3, 100, 25, c.BOTH); placeComponent(btn2, 0, 1, 2, 1, 50, 50, c.BOTH); placeComponent(btn3, 1, 1, 1, 2, 50, 25, c.BOTH); placeComponent(btn4, 1, 2, 2, 1, 25, 50, c.BOTH); placeComponent(btn5, 2, 2, 1, 1, 25, 25, c.BOTH); placeComponent(btn6, 0, 3, 1, 1, 50, 25, c.BOTH); placeComponent(btn7, 2, 3, 1, 1, 25, 25, c.BOTH); } // end setUpFrm public void placeComponent( Component comp, int x, int y, int height, int width, int xPerc, int yPerc, int fill){ c.gridx = x; c.gridy = y; c.gridheight = height; c.gridwidth = width; c.weightx = xPerc; c.weighty = yPerc; c.fill = fill; gbl.setConstraints (comp, c); frmGBL.add(comp); } // end placeComponent } // end class def