论坛首页 Java企业应用论坛

设置鼠标为等待状态后,如何使得鼠标和键盘无效?

浏览 2117 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2008-08-24  
OO
设置鼠标为等待状态后,如何使得鼠标和键盘无效?


当我们把鼠标设置成等待状态后,事实上,鼠标、键盘都还是可以使用的。

如何使得鼠标、键盘都无效呢?

1、对于鼠标我们可以这样:

递归得到此组件的父组件,一直得到的JRootPane。然后在JRootPane的glassPane上设置

glass.setVisible(true);
glass.addMouseListener(new MouseAdapter() {});
glass.addMouseMotionListener(new MouseMotionAdapter() {});

这样可以使得鼠标事件被glassPane所截获,从而使得点击无效。

2、但是,使得键盘无效要如何设置呢?

仿造1中,加入键盘事件的空实现,

glass.setVisible(true);
glass.addKeyListener(new KeyAdapter() {});

但是,很不幸,无法截获键盘事件。键盘依然可以使用?所以,对于键盘要如何截获呢?

知道的兄弟,还望不吝赐教!!!谢谢,呵呵




另外还有个问题,看书上的例子时,有这么一句

// Register for all the mouse events in the glass pane (and do nothing).
// This registration keeps the components in the content pane from being
// clickable. We could have created our own panel that called
// enableEvents(AWTEvent.MOUSE_EVENT_MASK |
// AWTEvent.MOUSE_MOTION_EVENT_MASK) to get the same effect.
glass.addMouseListener(new MouseAdapter() {});
glass.addMouseMotionListener(new MouseMotionAdapter() {});

此处的注释是什么意思呢?难道是我们继承JPanel,然后调用enableEvents()方法,这样就可以截获鼠标事件了嘛?此处的enableEvents()是啥意思??


刚才本来想发帖,结果发到提问里面去了。所以在这里在发个帖(应该是允许的吧,呵呵。在这高手云集的地方,发个帖都心虚)
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics