鼠标适配器的范围 [英] scope of the mouse adapter

查看:26
本文介绍了鼠标适配器的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在这种情况下 MouseAdapter 的作用域是什么.

I am wonder what the scope of the MouseAdapter is in this case.

class foo extends JPanel()
{
  private JMenu edit = new JMenu();
  public foo()
  {
      this.edit.getItem(0).addMouseListener(new MouseAdapter(){ 
          @Override
          public void mouseClicked(MouseEvent e) {
              if (e.getClickCount() == 1) {
                  edit.getItem(0).setEnabled(true);
              }
          } 
      });
  }
}

我认为 MouseAdapter 可以访问变量 edit,因为新声明的 MouseAdapter 是 foo 类的内部类.但是,它找不到变量edit.如果我显式声明了一个内部类并实现了 MouseAdapter 接口或其他任何东西,它可以从其中检测到变量 edit.所以我的问题是 new MouseAdpater() 的作用域是什么 有吗?此外,有没有人知道这方面的好书?非常感谢.顺便说一句,我得到的错误是局部变量是从内部类访问的,需要将其声明为final

I thought the MouseAdapter has access to the variable edit because the newly declared MouseAdapter is an inner class of the class foo. However, it can't find the variable edit. If I explicitly declare an inner class and implements, say, the MouseAdapter interface or whatever, it can detect the variable edit from within it.So my question is what the scope the new MouseAdpater() has? Besides, does anyone know a good read on this? Thank you very much. By the way, the error I got was the local variable was accessed from an inner class, needs to declare it final

推荐答案

1) edit.getItem(0) 如果存在则返回第一个 JMenuItem,否则返回 IllegalArgumentException

1) edit.getItem(0) returns fist JMenuItem if exist, otherwise returns IllegalArgumentException

2) this.edit.getItem(0),不是返回成员的类

2) this.edit.getItem(0), not class that returns members

3) edit.getItem(0).addMouseListener(new MouseAdapter(){ 适得其反,因为 JMenuJMenuItem 已经实现了 MouseEvents 正确,为了更好的解决方法,您必须查看 ButtonModel

3) edit.getItem(0).addMouseListener(new MouseAdapter(){ is contraproductive, becuase JMenu, JMenuItem has implemented MouseEvents correctly, for better workaround you have to look at ButtonModel

4) 没有理由鼠标适配器的范围

5) 要从 JMenu(不是 JMenuItem)监听事件,请查看 MenuListener

5) for listening of events from JMenu (not JMenuItem) look at MenuListener

这篇关于鼠标适配器的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆