class ColorAction implements ActionListener
{
private Color backgroundColor;
ButtonPanel bp;
public ColorAction(Color c, ButtonPanel bp)
{
backgroundColor=c;
this.bp=bp;//this是当前对象的指针
}
public void actionPerformed(ActionEvent event)
{
bp.setBackground(Color.yellow);//错误出现处~~
}
}
{
private Color backgroundColor;
ButtonPanel bp;
public ColorAction(Color c, ButtonPanel bp)
{
backgroundColor=c;
this.bp=bp;//this是当前对象的指针
}
public void actionPerformed(ActionEvent event)
{
bp.setBackground(Color.yellow);//错误出现处~~
}
}
……
public ButtonPanel(){
……
ColorAction yellowAction=new ColorAction(Color.yellow,this);
ColorAction blueAction=new ColorAction(Color.blue,this);
ColorAction redAction=new ColorAction(Color.red,this);
……
}
……
}
{
bp.setBackground(c);//错误出现处~~
}
试试可以么
如果不行,要么就实现三个监听器,不同监听器改变不同颜色
要么在监听器中,判断发生事件的控件,然后根据判断改变不同颜色,好像用getSource()得到产生事件的控件