import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class GridLayout1 extends Applet { private Button button1, button2, button3, button4, button5, button6; TextField display; public void init() { // layout of the applet is a BorderLayout using the North and Center cells setLayout(new BorderLayout()); // the north cell has only one component, a textfield display = new TextField(20); display.setEditable(false); add(display, BorderLayout.NORTH); // the center cell has a Panel with a GridLayout of 6 buttons Panel buttonsPanel = new Panel(); button1 = new Button("Button 1"); button2 = new Button("Button 2"); button3 = new Button("Button 3"); button4 = new Button("Button 4"); button5 = new Button("Button 5"); button6 = new Button("Button 6"); ButtonHandler handler = new ButtonHandler(this); button1.addActionListener(handler); button2.addActionListener(handler); button3.addActionListener(handler); button4.addActionListener(handler); button5.addActionListener(handler); button6.addActionListener(handler); buttonsPanel.setLayout(new GridLayout(3,2)); buttonsPanel.add(button1); buttonsPanel.add(button2); buttonsPanel.add(button3); buttonsPanel.add(button4); buttonsPanel.add(button5); buttonsPanel.add(button6); add(buttonsPanel, BorderLayout.CENTER); } } class ButtonHandler implements ActionListener { GridLayout1 applet; public ButtonHandler(GridLayout1 a) { applet=a; } public void actionPerformed(ActionEvent e) { // for each button, display the button label in the textfield applet.display.setText( ((Button)e.getSource()).getLabel()); } }