使用了两个匿名inner类。
你可以把它该成非匿名inner类,就好懂了。用匿名主要是简洁。
   jb2.addActionListener(new ActionListener2(){
      public void actionPerformed(ActionEvent e){
        if(jb.isEnabled()) {
          jb.setEnabled(false);
          jb2.setText("Enable");
        } else {
          jb.setEnabled(true);
          jb2.setText("Disable");
        }
      }
    });改成:
public A implements ActionListener
{
      public void actionPerformed(ActionEvent e){
        if(jb.isEnabled()) {
          jb.setEnabled(false);
          jb2.setText("Enable");
        } else {
          jb.setEnabled(true);
          jb2.setText("Disable");
        }
      }
}
ActionListener a = new A();
jb2.addActionListener(a);

解决方案 »

  1.   

    对不起,上面的public应该是class.
      

  2.   

    我曾经把它改成非匿名类,但还是有错误
    关键是有时搞不清楚他们之间的继承关系
    不知道这些错误怎么产生的,inner类不是可以无限制的访问outer类吗?
    这段代码的错误是:C:\Program Files\Xinox Software\JCreator Pro\MyProjects\thinkinginjava\thinkinginjava\P531.java:32: local variable mad is accessed from within inner class; needs to be declared final
            if(mad) {
               ^
    C:\Program Files\Xinox Software\JCreator Pro\MyProjects\thinkinginjava\thinkinginjava\P531.java:33: local variable faces is accessed from within inner class; needs to be declared final
              jb.setIcon(faces[3]);
                         ^
    C:\Program Files\Xinox Software\JCreator Pro\MyProjects\thinkinginjava\thinkinginjava\P531.java:33: local variable jb is accessed from within inner class; needs to be declared final
              jb.setIcon(faces[3]);
              ^
    C:\Program Files\Xinox Software\JCreator Pro\MyProjects\thinkinginjava\thinkinginjava\P531.java:34: local variable mad is accessed from within inner class; needs to be declared final
              mad = false;
              ^
    C:\Program Files\Xinox Software\JCreator Pro\MyProjects\thinkinginjava\thinkinginjava\P531.java:36: local variable faces is accessed from within inner class; needs to be declared final
              jb.setIcon(faces[0]);
                         ^
    C:\Program Files\Xinox Software\JCreator Pro\MyProjects\thinkinginjava\thinkinginjava\P531.java:36: local variable jb is accessed from within inner class; needs to be declared final
              jb.setIcon(faces[0]);
              ^
    C:\Program Files\Xinox Software\JCreator Pro\MyProjects\thinkinginjava\thinkinginjava\P531.java:37: local variable mad is accessed from within inner class; needs to be declared final
              mad = true;
              ^
    C:\Program Files\Xinox Software\JCreator Pro\MyProjects\thinkinginjava\thinkinginjava\P531.java:39: local variable jb is accessed from within inner class; needs to be declared final
            jb.setVerticalAlignment(JButton.TOP);
            ^
    C:\Program Files\Xinox Software\JCreator Pro\MyProjects\thinkinginjava\thinkinginjava\P531.java:40: local variable jb is accessed from within inner class; needs to be declared final
            jb.setHorizontalAlignment(JButton.LEFT);
            ^
    C:\Program Files\Xinox Software\JCreator Pro\MyProjects\thinkinginjava\thinkinginjava\P531.java:50: cannot resolve symbol
    symbol  : class ActionListener2 
    location: class P531
        jb2.addActionListener(new ActionListener2(){
                                  ^
    10 errorsProcess completed.
      

  3.   

    inner class引用的field必须定义为final
      

  4.   

    inner类是可以访问out类里的变量,但只能访问全局变量,怎么可能去访问函数里的局部变量呢
      

  5.   

    留下email,给你发点有关inner类的资料,所得很详细了!不大,就10页左右]
      

  6.   

    我的email是[email protected]
    谢谢
      

  7.   

    能能也给我发一份呀,谢谢你了。
    [email protected]
      

  8.   

    这个问题我已经解决了
    错误出在一些变量的定义的地点
    要把它设为全局变量就可以了
    以下是修改的通过的程序:
     import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    public class P531{
      boolean mad = false;
      String  path = "C:/Documents and Settings/xxxxxxx/My Documents/My Pictures/";  
      Icon[] faces = {
          new ImageIcon(path + "004.jpg"),
          new ImageIcon(path + "03.jpg"),
          new ImageIcon(path + "05.jpg"),
          new ImageIcon(path + "011.jpg"),
          new ImageIcon(path + "007.jpg"),
      };  
      JButton
          jb = new JButton("JButton", faces[3]),
          jb2 = new JButton("Disable");  
      public static void main(String[] args){
       //JFrame jframe=new JFrame();
       //boolean mad = false;
       //JButton jb,jb2;
       P531 example=new P531();
       example.go();
      }
      public void go(){
       //boolean mad = false;
        JFrame jframe=new JFrame("zhanlinghua");
            jframe.getContentPane().setLayout(new FlowLayout());
        jb.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
            if(mad) {
              jb.setIcon(faces[3]);
              mad = false;
            } else {
              jb.setIcon(faces[0]);
              mad = true;
            }
            jb.setVerticalAlignment(JButton.TOP);
            jb.setHorizontalAlignment(JButton.LEFT);
          }
        });          jb.setRolloverEnabled(true);
        jb.setRolloverIcon(faces[1]);
        jb.setPressedIcon(faces[2]);
        jb.setDisabledIcon(faces[4]);
        jb.setToolTipText("Yow!");
        jframe.getContentPane().add(jb);
        jb2.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
            if(jb.isEnabled()) {
              jb.setEnabled(false);
              jb2.setText("Enable");
            } else {
              jb.setEnabled(true);
              jb2.setText("Disable");
            }
          }
        });
        jframe.getContentPane().add(jb2);
        jframe.setSize(500,500);
        jframe.setVisible(true);   
    }
    }