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