1  import java.applet.Applet;
  2  import java.awt.*;
  3  import java.awt.event.*;
  4  
  5  public class GridLayout1 extends Applet 
  6  {
  7    private Button button1, button2, button3, button4, button5, button6;
  8    TextField display;
  9    
 10    public void init() 
 11    { // layout of the applet is a BorderLayout using the North and Center cells
 12      setLayout(new BorderLayout());
 13  
 14      // the north cell has only one component, a textfield
 15      display = new TextField(20);
 16      display.setEditable(false);
 17      add(display, BorderLayout.NORTH);
 18  
 19      // the center cell has a Panel with a GridLayout of 6 buttons
 20      Panel buttonsPanel = new Panel();
 21      button1 = new Button("Button 1");
 22      button2 = new Button("Button 2");
 23      button3 = new Button("Button 3");
 24      button4 = new Button("Button 4");
 25      button5 = new Button("Button 5");
 26      button6 = new Button("Button 6");
 27  
 28      ButtonHandler handler = new ButtonHandler(this);
 29      button1.addActionListener(handler);
 30      button2.addActionListener(handler);
 31      button3.addActionListener(handler);
 32      button4.addActionListener(handler);
 33      button5.addActionListener(handler);
 34      button6.addActionListener(handler);
 35     
 36      buttonsPanel.setLayout(new GridLayout(3,2));
 37      buttonsPanel.add(button1);
 38      buttonsPanel.add(button2);
 39      buttonsPanel.add(button3);
 40      buttonsPanel.add(button4);
 41      buttonsPanel.add(button5);
 42      buttonsPanel.add(button6);
 43  
 44      add(buttonsPanel, BorderLayout.CENTER);
 45    }
 46  }
 47      
 48    
 49  class ButtonHandler implements ActionListener 
 50  {
 51    GridLayout1 applet;
 52  
 53    public ButtonHandler(GridLayout1 a)  
 54    { applet=a; }  
 55  
 56    public void actionPerformed(ActionEvent e)
 57    {
 58      // for each button, display the button label in the textfield
 59      applet.display.setText( ((Button)e.getSource()).getLabel());
 60    }
 61  }