public class ActionTester
1: import java.awt.*;
2: import java.awt.event.*;
3: import javax.swing.*;
5: public class ActionTester
6: {
7: public static void main(String[] args)
8: {
9: JFrame frame = new JFrame();
11: final int FIELD_WIDTH = 20;
12: textField = new JTextField(FIELD_WIDTH);
13: textField.setText("Click a button!");
15: JButton helloButton = new JButton("Say Hello");
17: helloButton.addActionListener(
18: createGreetingButtonListener("Hello, World!"));
20: JButton goodbyeButton = new JButton("Say Goodbye");
22: goodbyeButton.addActionListener(
23: createGreetingButtonListener("Goodbye, World!"));
25: frame.setLayout(new FlowLayout());
27: frame.add(helloButton);
28: frame.add(goodbyeButton);
29: frame.add(textField);
31: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
32: frame.pack();
33: frame.setVisible(true);
34: }
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: }
49: private static JTextField textField;
50: }