你的background变量定义在了JPanel里面,应该在D和E中各自定义一个:
...
private class D implements ActionListener
{
private Color background;
...
}
private class E implements ActionListener
{
private Color background;
...
}
...
private class D implements ActionListener
{
private Color background;
...
}
private class E implements ActionListener
{
private Color background;
...
}
import java.awt.event.*;
import javax.swing.*;public class A
{
public static void main(String[] args)
{
B b=new B();
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.show();
}
}
class B extends JFrame
{
public B()
{
setTitle("我靠");
setSize(W,H); C c=new C();
Container content=getContentPane();
content.add(c);
}
public static final int W=300;
public static final int H=200;
}
class C extends JPanel
{
public C()
{
JButton button1=new JButton("bule");
JButton button2=new JButton("white"); add(button1);
add(button2); D action1=new D(Color.BLUE);
button1.addActionListener(action1); E action2=new E(Color.white);
button2.addActionListener(action2);
}
private class D implements ActionListener
{
public D(Color d)
{
backgroundd=d;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundd);
repaint();
}
} private class E implements ActionListener
{
public E(Color e)
{
backgrounde=e;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgrounde);//分别设成自己的color
repaint();
}
}
private Color backgrounde;
private Color backgroundd;//add
}
button1.addActionListener(action1);
E action2=new E(Color.white);
button2.addActionListener(action2);
这两句出错,E和D类的构造函数需要2个Color对象做参数!!