首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java中mouseListener和mouseMotionListener的区别?

Java中mouseListener和mouseMotionListener的区别?
EN

Stack Overflow用户
提问于 2009-11-26 01:27:57
回答 2查看 8.5K关注 0票数 4

当鼠标移动到组件上时,mouseMotionListener是否会触发事件,而mouseListener只会在我按下按钮时触发?

所以如果我只有一个mousePressed事件,那么我就不需要mouseMotionListener了?如果我有mouseEntered或mouseExited的话?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-11-26 01:30:39

是的,你是对的。当鼠标移动到“热点”上时,mouseMotionListener用于执行操作。

here就是一个很好的例子

在处理mousePressed事件时,您只需要mousePressed事件,除非您想要在鼠标悬停时添加更多要执行的事件。

票数 5
EN

Stack Overflow用户

发布于 2009-11-26 01:33:19

它们监听不同的事件:

MouseListener

代码语言:javascript
复制
mouseClicked(MouseEvent event)   // Called just after the user clicks the listened-to component.
mouseEntered(MouseEvent event)   // Called just after the cursor enters the bounds of the listened-to component.
mouseExited(MouseEvent event)    // Called just after the cursor exits the bounds of the listened-to component.
mousePressed(MouseEvent event)   // Called just after the user presses a mouse button while the cursor is over the listened-to component.
mouseReleased(MouseEvent event)  // Called just after the user releases a mouse button after a mouse press over the listened-to component

MouseMotionListener

代码语言:javascript
复制
mouseDragged(MouseEvent event)   // Called in response to the user moving the mouse while holding a mouse button down. This event is fired by the component that fired the most recent mouse-pressed event, even if the cursor is no longer over that component.
mouseMoved(MouseEvent event)     // Called in response to the user moving the mouse with no mouse buttons pressed. This event is fired by the component that's currently under the cursor.

根据你想要的事件添加监听器。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1798582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档