简体   繁体   English

Java双击(单击除外)

[英]Java double click excluding singleclick

Can anybody tell my how can I fix this... 谁能告诉我该如何解决...

@Override
public void mouseClicked(MouseEvent me) {
    super.mouseClicked(me);
        if (me.getClickCount() >= 2) {
            System.out.println("double click");
        }else{
            System.out.println("single click");
    }
}

When I doubleclick on my component in row wihout any time between clicking,it writes in console first " single click " and then " double click ". 当我在单击之间的任何时候双击行中的组件时,它首先在控制台中写入“ 单击 ”,然后“ 双击 ”。 Thanks for answers. 感谢您的回答。

You cannot avoid this the way you are doing it right now. 您无法立即避免这样做。

When you first click, the mouseClicked event is called immediatly and doesn't wait for the second click to execute. 首次单击时,将立即调用mouseClicked事件,而不会等待第二次单击执行。 That is why "single click" is displayed. 这就是为什么显示“单击”的原因。

A way of doing it would be using a timer . 一种方法是使用计时器 Start the timer after the click, and if there is no 2nd click before 1 sec (or any time you chose) then consider as single click but if there is a second one consider it as a double click. 单击后启动计时器,如果在1秒(或您选择的任何时间)之前没有第二次单击,则将其视为单击,但如果第二次则将其视为双击。 That solution demands a little bit of thinking about how to implement, but not impossible I guess. 该解决方案需要对如何实现进行一些思考,但我想并不是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM