你的background变量定义在了JPanel里面,应该在D和E中各自定义一个:
...
private class D implements ActionListener
{
    private Color background;
    ...
}
private class E implements ActionListener
{
    private Color background;
    ...
}

解决方案 »

  1.   

    我想这与内部类的数量并没有必然关系。只不过被你写成这种样子,setBackground和repaint让编译器误以为是内部类的,所以没有成功。你在他们之前加上需要的frame或者panel应该就可以了。
      

  2.   

    import java.awt.*;
    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
    }
      

  3.   

    刚才没仔细看程序就抢着发了,看过之后的确是因为你的background没有声明为各自类的全局变量所致。
      

  4.   

    D action1=new D(Color.bule);
    button1.addActionListener(action1);

    E action2=new E(Color.white);
    button2.addActionListener(action2);
    这两句出错,E和D类的构造函数需要2个Color对象做参数!!