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 = 400;
12: public static final int HEIGHT = 300;
13:
14: public ButtonDemo( )
15: {
16: setSize(WIDTH, HEIGHT);
17: WindowDestroyer listener = new WindowDestroyer( );
18: addWindowListener(listener);
19:
20: Container contentPane = getContentPane( );
21: contentPane.setBackground(Color.WHITE);
22:
23: contentPane.setLayout(new FlowLayout( ));
24:
25: JButton sunnyButton = new JButton("Sunny");
26: sunnyButton.addActionListener(this);
27: contentPane.add(sunnyButton);
28:
29: JButton cloudyButton = new JButton("Cloudy");
30: cloudyButton.addActionListener(this);
31: contentPane.add(cloudyButton);
32: }
33:
34: public void actionPerformed(ActionEvent e)
35: {
36: String actionCommand = e.getActionCommand( );
37: Container contentPane = getContentPane( );
38:
39: if (actionCommand.equals("Sunny"))
40: contentPane.setBackground(Color.BLUE);
41: else if (actionCommand.equals("Cloudy"))
42: contentPane.setBackground(Color.GRAY);
43: else
44: System.out.println("Error in button interface.");
45: }
46: }
47:
48: