关于java鼠标事件处理的一个问题

顾俊程 发布于 2011/08/29 10:37
阅读 1K+
收藏 0
btn.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount() == 2)
{
System.out.println("鼠标单击了两次") ;
}
else
{
System.out.println("点击鼠标") ;
}
}
}
) ;
对于如下方法当我双击按钮时,为什么else后面的语句也会执行阿?
加载中
0
穿着马甲的鸟
穿着马甲的鸟
System.out.println 一下  e.getClickCount() 看看呗。
0
游而不击
游而不击
貌似有doubleclick事件的吧。
0
ShaoJiahao
ShaoJiahao
mouseClicked 应该是响应单击事件
0
曾赛
曾赛

如果你的输出结果是:
点击鼠标
鼠标单击了两次

那么可能是以下原因:鼠标每次单击都会产生 mouseClicked 事件,但第一次产生时,clickCount 不是2而是1,此时会输出 else 语句。

详细情况请看:

 if the first mouse button is pressed, events are sent in the following order:

    id              modifiers    button           
    MOUSE_PRESSED:  BUTTON1_MASK BUTTON1
    MOUSE_RELEASED: BUTTON1_MASK BUTTON1
    MOUSE_CLICKED:  BUTTON1_MASK BUTTON1
 
When multiple mouse buttons are pressed, each press, release, and click results in a separate event.
For example, if the user presses button 1 followed by button 2, and then releases them in the same order, the following sequence of events is generated:

    id              modifiers    button           
    MOUSE_PRESSED:  BUTTON1_MASK BUTTON1
    MOUSE_PRESSED:  BUTTON2_MASK BUTTON2
    MOUSE_RELEASED: BUTTON1_MASK BUTTON1
    MOUSE_CLICKED:  BUTTON1_MASK BUTTON1
    MOUSE_RELEASED: BUTTON2_MASK BUTTON2
    MOUSE_CLICKED:  BUTTON2_MASK BUTTON2
 
If button 2 is released first, the MOUSE_RELEASED/MOUSE_CLICKED pair for BUTTON2_MASK arrives first, followed by the pair for BUTTON1_MASK.
参考来自: http://download.oracle.com/javase/1.4.2/docs/api/java/awt/event/MouseEvent.html#getClickCount()

0
Loblood
Loblood

mouseClicked是监听鼠标单击事件的吧。

java中所谓的单击事件我记得是指“鼠标按下去并且抬起来”这一整个过程称作一次单击。然后么,当你双击了鼠标以后,无论多快么总会被按钮监听到一个单击事件的吧。

也就是说你if..else..中else中的相关代码被执行了么。我觉得可以定义一个定时器来区别单双击- -

返回顶部
顶部