01: import java.awt.*;
02: import java.awt.event.*;
03: import javax.swing.*;
04: 
05: public class ActionTester
06: {
07:    public static void main(String[] args)
08:    {
09:       JFrame frame = new JFrame();
10: 
11:       final int FIELD_WIDTH = 20;
12:       textField = new JTextField(FIELD_WIDTH);
13:       textField.setText("Click a button!");
14: 
15:       JButton helloButton = new JButton("Say Hello");
16: 
17:       helloButton.addActionListener(
18:          createGreetingButtonListener("Hello, World!"));
19: 
20:       JButton goodbyeButton = new JButton("Say Goodbye");
21: 
22:       goodbyeButton.addActionListener(
23:          createGreetingButtonListener("Goodbye, World!"));
24: 
25:       frame.setLayout(new FlowLayout());
26: 
27:       frame.add(helloButton);
28:       frame.add(goodbyeButton);
29:       frame.add(textField);
30: 
31:       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
32:       frame.pack();
33:       frame.setVisible(true);
34:    }
35: 
36:    public static ActionListener createGreetingButtonListener(
37:       final String message)
38:    {
39:       return new
40:          ActionListener()
41:          {
42:             public void actionPerformed(ActionEvent event)
43:             {
44:                textField.setText(message);
45:             }
46:          };
47:    }
48: 
49:    private static JTextField textField;
50: }