//GBL1.java //Demonstrate the GridbagLayout //Andy Harris, 05/00 import java.awt.*; import java.applet.*; import java.awt.event.*; public class GBL1 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); //-------------- button one -------------- c.gridx = 0; c.gridy = 0; c.gridheight = 1; c.gridwidth = 3; c.fill = c.BOTH; c.weightx = 50; c.weighty = 25; gbl.setConstraints (btn1, c); frmGBL.add(btn1); //-------------- button two -------------- c.gridx = 0; c.gridy = 1; c.gridheight = 2; c.gridwidth = 1; c.fill = c.BOTH; c.weightx = 50; c.weighty = 100; gbl.setConstraints (btn2, c); frmGBL.add(btn2); //-------------- button three -------------- c.gridx = 1; c.gridy = 1; c.gridheight = 1; c.gridwidth = 2; c.fill = c.BOTH; c.weightx = 50; c.weighty = 25; gbl.setConstraints (btn3, c); frmGBL.add(btn3); //-------------- button four -------------- c.gridx = 1; c.gridy = 2; c.gridheight = 2; c.gridwidth = 1; c.fill = c.BOTH; c.weightx = 25; c.weighty = 50; gbl.setConstraints (btn4, c); frmGBL.add(btn4); //-------------- button five -------------- c.gridx = 2; c.gridy = 2; c.gridheight = 1; c.gridwidth = 1; c.fill = c.BOTH; c.weightx = 25; c.weighty = 25; gbl.setConstraints (btn5, c); frmGBL.add(btn5); //-------------- button six -------------- c.gridx = 0; c.gridy = 3; c.gridheight = 1; c.gridwidth = 1; c.fill = c.BOTH; c.weightx = 50; c.weighty = 25; gbl.setConstraints (btn6, c); frmGBL.add(btn6); //-------------- button seven -------------- c.gridx = 2; c.gridy = 3; c.gridheight = 1; c.gridwidth = 1; c.fill = c.BOTH; c.weightx = 25; c.weighty = 25; gbl.setConstraints (btn7, c); frmGBL.add(btn7); } // end setUpFrm } // end class def