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