“……fileMenu_Exit.addActionListener(this); 
     JMenu editMenu = new JMenu("编辑(E)", true);editMenu.setMnemonic('E'); 
  editMenu.addActionListener(new ActionListener()
   {
    public void actionPerformed(ActionEvent e){
     checkMenuItemEnabled();//设置剪切、复制、粘贴、删除等功能的可用性
    }
   }
  );……”我在看黄斌编的记事本原代码时,觉得有些不明白: 1,fileMenu_Exit调用addActionListener方法没什么的,可以理解,因为它确实有Exit可以执行的,可属于JMenu类的editMenu对象有什么必要调用这个方法呢??2,在上面editMenu的调用过程时,为什么又与fileMenu_Exit调用时使用的参数不同,一个是直接用this(我对这个this在这的使用也不明白的),另一个却用另一方式,我想部这两种不同的调用有什么本质上的区别????
 

解决方案 »

  1.   

    fileMenu_Exit.addActionListener(this); 
    ~~~~~~~~~~~~~~~~~~~~
    说明this(就是当前类)实现了ActionListener接口,它会调用该类中的actionPerformed(ActionEvent e)方法来执行菜单动作。而
    editMenu.addActionListener(new ActionListener()
       {
        public void actionPerformed(ActionEvent e){
         checkMenuItemEnabled();//设置剪切、复制、粘贴、删除等功能的可用性
        }
       }
      );
    调用这里的actionPerformed(ActionEvent e)方法。以上两种写法本质上是一样的,都是给菜单一个listener,执行的时候找到相应的actionPerformed()方法去执行。
    二者没有必然联系,LZ只需搞明白接口实现的相关概念就清楚了。