public class ButtonDemo extends JFrame implements ActionListener
1: //ButtonDemo.java
2:
3: import javax.swing.*;
4: import java.awt.*;
5: import java.awt.event.*;
6:
7: /**
8: Simple demonstration of putting buttons in a JFrame.
9: */
10: public class ButtonDemo extends JFrame implements ActionListener
11: {
12: public static final int WIDTH = 400;
13: public static final int HEIGHT = 300;
14:
15: public ButtonDemo( )
16: {
17: setSize(WIDTH, HEIGHT);
18: WindowDestroyer listener = new WindowDestroyer( );
19: addWindowListener(listener);
20:
21: Container contentPane = getContentPane( );
22: contentPane.setBackground(Color.WHITE);
23:
24: contentPane.setLayout(new FlowLayout( ));
25:
26: JButton sunnyButton = new JButton("Sunny");
27: sunnyButton.addActionListener(this);
28: contentPane.add(sunnyButton);
29:
30: JButton cloudyButton = new JButton("Cloudy");
31: cloudyButton.addActionListener(this);
32: contentPane.add(cloudyButton);
33: }
34:
35: public void actionPerformed(ActionEvent e)
36: {
37: String actionCommand = e.getActionCommand( );
38: Container contentPane = getContentPane( );
39:
40: if (actionCommand.equals("Sunny"))
41: contentPane.setBackground(Color.BLUE);
42: else if (actionCommand.equals("Cloudy"))
43: contentPane.setBackground(Color.GRAY);
44: else
45: System.out.println("Error in button interface.");
46: }
47: }
48: