public class SetActionCommandDemo extends JFrame implements ActionListener
1: import javax.swing.*;
2: import java.awt.*;
3: import java.awt.event.*;
4:
5: /**
6: Simple demonstration of putting buttons in a JFrame.
7: */
8: public class SetActionCommandDemo extends JFrame implements ActionListener
9: {
10: public static final int WIDTH = 300;
11: public static final int HEIGHT = 200;
12:
13: /**
14: Creates and displays a window of the class SetActionCommandDemo.
15: */
16: public static void main(String[] args)
17: {
18: SetActionCommandDemo buttonGui = new SetActionCommandDemo();
19: buttonGui.setVisible(true);
20: }
21:
22: public SetActionCommandDemo()
23: {
24: setSize(WIDTH, HEIGHT);
25:
26: addWindowListener(new WindowDestroyer());
27: setTitle("SetActionCommand Demo");
28: Container contentPane = getContentPane();
29: contentPane.setBackground(Color.BLUE);
30:
31: contentPane.setLayout(new FlowLayout());
32:
33: JButton stopButton = new JButton("Red");
34: stopButton.setActionCommand("Stop");
35: stopButton.addActionListener(this);
36: contentPane.add(stopButton);
37:
38: JButton goButton = new JButton("Green");
39: goButton.setActionCommand("Go");
40: goButton.addActionListener(this);
41: contentPane.add(goButton);
42: }
43:
44: public void actionPerformed(ActionEvent e)
45: {
46: Container contentPane = getContentPane();
47:
48: if (e.getActionCommand().equals("Stop"))
49: contentPane.setBackground(Color.RED);
50: else if (e.getActionCommand().equals("Go"))
51: contentPane.setBackground(Color.GREEN);
52: else
53: System.out.println("Error in button interface.");
54: }
55: }