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

iceman1952 2008-08-24 06:37:52
设置鼠标为等待状态后,如何使得鼠标和键盘无效?


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

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

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

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

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

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

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

仿造一种,加入键盘事件的空实现,

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()是啥意思??

...全文
338 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yfyht 2008-08-28
  • 打赏
  • 举报
回复
为什么要让键盘无效那?鼠标和键盘都无效了,你还做什么?连退出都不行。
healer_kx 2008-08-28
  • 打赏
  • 举报
回复
我主要是来接分的,.呵呵,我不是JavaEye上面的高手...
小宏 2008-08-27
  • 打赏
  • 举报
回复
关注...

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧