我在写关于时间监听的小程序,但是不知道为什么只能显示一种颜色,不能换颜色,代码如下:
package com.panmin.ActionDemo;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class ButtonTest { public static void main(String[] args) {

ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}}
class ButtonFrame extends JFrame {
public ButtonFrame(){
setTitle("事件监听");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

ButtonPanel panel = new ButtonPanel();
add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 300;

}
class ButtonPanel extends JPanel {
public ButtonPanel(){
JButton redButton = new JButton("RED");
JButton yellowButton = new JButton("YELLO");
JButton blueButton = new JButton("BLUE");

add(redButton);
add(yellowButton);
add(blueButton);


ColorAction yellow = new ColorAction(Color.YELLOW);
ColorAction red = new ColorAction(Color.RED);
ColorAction blue = new ColorAction(Color.BLUE);

redButton.addActionListener(red);
yellowButton.addActionListener(yellow);
blueButton.addActionListener(blue);

}

private class ColorAction implements ActionListener {
public ColorAction( Color c){
color = c;
}
public void actionPerformed(ActionEvent e) {

setBackground(color);
}
}
private Color color;
}

解决方案 »

  1.   

    color放在ButtonFrame里面了,结果最后被赋值为Color.BLUE
    应该为
    setBackground(color);
    }
    private Color color;
    }
    }
    注意缩进。。
      

  2.   


    package com.xiaoyong;import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class ButtonTest { public static void main(String[] args) { ButtonFrame frame = new ButtonFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }}@SuppressWarnings("serial")
    class ButtonFrame extends JFrame {
    public ButtonFrame() {
    setTitle("事件监听");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); ButtonPanel panel = new ButtonPanel();
    add(panel);
    } public static final int DEFAULT_WIDTH = 300;
    public static final int DEFAULT_HEIGHT = 300;}@SuppressWarnings("serial")
    class ButtonPanel extends JPanel {
    public ButtonPanel() {
    JButton redButton = new JButton("RED");
    JButton yellowButton = new JButton("YELLO");
    JButton blueButton = new JButton("BLUE"); add(redButton);
    add(yellowButton);
    add(blueButton); ColorAction yellow = new ColorAction(Color.YELLOW);
    ColorAction red = new ColorAction(Color.RED);
    ColorAction blue = new ColorAction(Color.BLUE); redButton.addActionListener(red);
    yellowButton.addActionListener(yellow);
    blueButton.addActionListener(blue); } private class ColorAction implements ActionListener {
    private Color color; public ColorAction(Color c) {
    color = c;
    } public void actionPerformed(ActionEvent e) { setBackground(color);
    }
    }
    }
    这样就没问题了...
    PS:LZ发帖的时候注意,将java代码写入[ code=Java ][ /code ]中进行格式化显示
      

  3.   

    将[ code=java][ /code]中的第一个空格去掉。
      

  4.   


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class ButtonTest { public static void main(String[] args) { ButtonFrame frame = new ButtonFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }//}//class
    @SuppressWarnings("serial")
    class ButtonFrame extends JFrame {
    public ButtonFrame(){
    setTitle("事件监听");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); ButtonPanel panel = new ButtonPanel();
    add(panel);
    }//
    public static final int DEFAULT_WIDTH = 300;
    public static final int DEFAULT_HEIGHT = 300;}//class
    @SuppressWarnings("serial")
    class ButtonPanel extends JPanel {
    public ButtonPanel(){
    JButton redButton = new JButton("RED");
    JButton yellowButton = new JButton("YELLO");
    JButton blueButton = new JButton("BLUE"); add(redButton);
    add(yellowButton);
    add(blueButton);
    ColorAction yellow = new ColorAction(Color.YELLOW);
    ColorAction red = new ColorAction(Color.RED);
    ColorAction blue = new ColorAction(Color.BLUE); redButton.addActionListener(red);
    yellowButton.addActionListener(yellow);
    blueButton.addActionListener(blue); }// private class ColorAction implements ActionListener {
    public ColorAction( Color c){
    color = c;
    }
    public void actionPerformed(ActionEvent e) { setBackground(color);
    }
    private Color color;
    }//



    }//classprivate Color color;
    写在了class ColorAction类的外面