public class TestButtonColor extends JFrame
1: //TestButtonColor.java
3: import java.awt.*;
4: import java.awt.event.*;
5: import javax.swing.*;
7: public class TestButtonColor extends JFrame
8: {
9: TestButtonColor(String s)
10: {
11: super(s);
12: }
14: public static void main(String[] args)
15: {
16: TestButtonColor app = new TestButtonColor("Testing Button Color");
17: app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
18: app.getContentPane().setLayout(null);
19: app.getContentPane().setBackground(Color.white);
21: final JButton button = new JButton();
22: button.setBackground(Color.red);
23: button.setBounds(20, 20, 80, 40);
24: app.getContentPane().add(button);
25: /*
26: final JLabel label = new JLabel("Hello!", SwingConstants.CENTER);
27: label.setOpaque(true);
28: label.setBackground(Color.red);
29: label.setBounds(120, 20, 50, 40);
30: app.getContentPane().add(label);
31: */
32: button.addActionListener(new ActionListener()
33: {
34: public void actionPerformed(ActionEvent e)
35: {
36: /*if (label.getBackground().equals(Color.red))
37: label.setBackground(Color.green);
38: else if (label.getBackground().equals(Color.green))
39: label.setBackground(Color.blue);
40: else if (label.getBackground().equals(Color.blue))
41: label.setBackground(Color.red);*/
42: if (e.getSource() == button && button.getBackground().equals(Color.red))
43: button.setBackground(Color.green);
44: else if (e.getSource() == button && button.getBackground().equals(Color.green))
45: button.setBackground(Color.blue);
46: else if (e.getSource() == button && button.getBackground().equals(Color.blue))
47: button.setBackground(Color.red);
48: }
49: });
50: app.setBounds(50, 100, 200, 100);
51: app.setVisible(true);
52: }
53: }