01: import java.awt.*;
02: import java.awt.event.*;
03: import javax.swing.*;
04:
05: public class ActionTest
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: Container contentPane = frame.getContentPane();
26: contentPane.setLayout(new FlowLayout());
27:
28: contentPane.add(helloButton);
29: contentPane.add(goodbyeButton);
30: contentPane.add(textField);
31:
32: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
33: frame.pack();
34: frame.show();
35: }
36:
37: public static ActionListener createGreetingButtonListener(
38: final String message)
39: {
40: return new
41: ActionListener()
42: {
43: public void actionPerformed(ActionEvent event)
44: {
45: textField.setText(message);
46: }
47: };
48: }
49:
50: private static JTextField textField;
51: }