Chris Johnson, Index
GridLayout - Simpler Application Example

/* 
Author: C.W. Johnson,
johnson@dcs.gla.ac.uk
Last modified: 05/01/2000

Presents a number of buttons using a grid layout to resemble the keys
on a calculator.   There is no zero button and the screen that presents
the numbers is editable.
*/

import java.awt.*;
import java.awt.event.*;
 
public class GridLayoutTest extends CloseableFrame2 implements ActionListener
{
	TextField screen= new TextField("0", 12);
 
	public GridLayoutTest()
	{
		add(screen, "North");
 
		keys = new Panel();
		keys.setLayout(new GridLayout(3, 3, 5, 5));
 
		addButton("1", keys);
		addButton("2", keys);
		addButton("3", keys);
		addButton("4", keys);
		addButton("5", keys);
		addButton("6", keys);
		addButton("7", keys);
		addButton("8", keys);
		addButton("9", keys);
		add(keys, "South");
	}
 
	public void addButton(String name, Panel container)
	{
		Button b = new Button(name);
		b.addActionListener(this);
		container.add(b);
	}
 
	public void actionPerformed(ActionEvent evt)
	{
		String arg = evt.getActionCommand();
		String text = screen.getText();
		screen.setText(text + arg);
	}
 
	public static void main(String [] args)
	{
		Frame f = new GridLayoutTest();
		f.setSize(400,200);
		f.show();
	}
 
	private Panel screen;
	private Panel keys;
}


forward