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