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: final JTextField textField = new JTextField(FIELD_WIDTH);
13: textField.setText("Click a button!");
15: JButton helloButton = new JButton("Say Hello");
17: helloButton.addActionListener(new
18: ActionListener()
19: {
20: public void actionPerformed(ActionEvent event)
21: {
22: textField.setText("Hello, World!");
23: }
24: });
26: JButton goodbyeButton = new JButton("Say Goodbye");
28: goodbyeButton.addActionListener(new
29: ActionListener()
30: {
31: public void actionPerformed(ActionEvent event)
32: {
33: textField.setText("Goodbye, World!");
34: }
35: });
37: frame.setLayout(new FlowLayout());
39: frame.add(helloButton);
40: frame.add(goodbyeButton);
41: frame.add(textField);
43: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
44: frame.pack();
45: frame.setVisible(true);
46: }
47: }