FileDocCategorySizeDatePackage
GridBagEx1.javaAPI DocExample2219Tue Jul 15 20:34:42 BST 2003None

GridBagEx1

public class GridBagEx1 extends Applet implements ActionListener

Fields Summary
Constructors Summary
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

		System.out.println("You pressed " + e.getActionCommand());
	
public voidinit()

		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		setFont(new Font("Helvetica", Font.PLAIN, 14));
		setLayout(gridbag);
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		makebutton("Button1", gridbag, c);
		makebutton("Button2", gridbag, c);
		makebutton("Button3", gridbag, c);
		c.gridwidth = GridBagConstraints.REMAINDER; //end row
		makebutton("Button4", gridbag, c);
		c.weightx = 0.0;                   //reset to the default
		makebutton("Button5", gridbag, c); //another row
		c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
		makebutton("Button6", gridbag, c);
		c.gridwidth = GridBagConstraints.REMAINDER; //end row
		makebutton("Button7", gridbag, c);
		c.gridwidth = 1;                   //reset to the default
		c.gridheight = 2;
		c.weighty = 1.0;
		makebutton("Button8", gridbag, c);
		c.weighty = 0.0;                   //reset to the default
		c.gridwidth = GridBagConstraints.REMAINDER; //end row
		c.gridheight = 1;                  //reset to the default
		makebutton("Button9", gridbag, c);
		makebutton("Button10", gridbag, c);
		setSize(300, 100);
	
public static voidmain(java.lang.String[] args)
If invoked as an application, start things up

		Frame f = new Frame("GridBag Layout Example");
		GridBagEx1 ex1 = new GridBagEx1();
		ex1.init();
		f.add("Center", ex1);
		f.pack();
		f.setSize(f.getPreferredSize());
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				System.exit(0);
			}
		});
	
protected voidmakebutton(java.lang.String name, java.awt.GridBagLayout gridbag, java.awt.GridBagConstraints c)

		Button button = new Button(name);
		button.addActionListener(this);
		add(button, c);