Also available as MoreEvents.java

/**
 * A Simple stand-alone GUI program based on Swing.
 *  
 * This example uses a class that extends JFrame
 * instead of creating a JFrame object!
 *
 * This class also implements ActionListener, so we 
 * can use it to handle Action events
 *
 * The main creates a window with a bunch of buttons 
 * and a textarea.
 * Uses a JPanel to hold the buttons (container component).
 *
 * Whenever the user does something that triggers an ActionEvent
 * (clicking on a Button), the textarea is used to
 * display what is happening.
 *
 * This sample is based (in part) on code supplied in
 * an example found at java.sun.com (how to write a mouse listener
 * tutorial).
 * @author Dave Hollinger
 * @version 1.0
 */

import javax.swing.*;
import java.awt.*;       // needed for Container
import java.awt.event.*;  // needed for event types (listeners)


class MoreEvents extends JFrame implements ActionListener {

  // The textfield must not be anonymouse, since we want to        
  // be able to modify it from event handlers

  JTextArea jt;

  // main just creates a new MoreEvents Object
  // (which is a special kind of JFrame).

  public static void main(String [] args) {
        MoreEvents me = new MoreEvents();
  }

  // Constructor sets up the window and gets things rolling.

  MoreEvents() {

        Container c = getContentPane();
        // set up layout manager
        c.setLayout(new BorderLayout(10,10));
        
        // add a button panel 
        c.add( createButtonPanel(), BorderLayout.NORTH );
        
        // add single text Area
        jt = new JTextArea(5,20);
        jt.setSize(100,20);
        jt.setFont(new Font("Arial",Font.BOLD,16));
        jt.setBackground(Color.WHITE);
        jt.setForeground(Color.BLUE);
        c.add(jt,BorderLayout.CENTER);

        // establish what happens when the window is closed
        // (without this the program would keep running!)
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // set the size
        setSize(200,200);
        // turn it on
        setVisible(true);
  }


  JPanel createButtonPanel() {
        // create a JPanel with a bunch of buttons in it arranged in
        // a grid.
        JPanel jp = new JPanel();

        // establish the layout manager
        // 2 rows, 3 cols everything 5 pixels apart
        jp.setLayout( new GridLayout(2,3,5,5));

        // add a bunch of buttons
        for (int i=0;i<6;i++) {
          JButton j = new JButton(new Integer(i).toString());
          // change the font
          j.setFont(new Font("Courier",Font.BOLD,20));

          // change the colors
          j.setBackground(Color.WHITE);
          j.setForeground(Color.BLACK);

          // set up the listener for this button
          // NOTE: We listen to ourself (we are an ActionListener!).
          j.addActionListener(this);

          // add the button to the panel
          jp.add(j);
        }
        return(jp);
  }

  // here is the method required as an ActionListener
  // (there is only one!)

  public void actionPerformed(ActionEvent e) {

        // find out what the action was
        // (for buttons, this is the button text by default)
        String act = e.getActionCommand();

        jt.setText("Action is " + act );
  }

}