Java:忽略单击双击? [英] Java : ignore single click on double click?

查看:126
本文介绍了Java:忽略单击双击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以想到一个好的方式来忽略在Java中双击的单击!

can anyone think of a good way to ignore the single click that comes with a double-click in Java ?

我希望每个人都有不同的行为:

I'm looking to have different behaviors for each such that:



  • 双击在屏幕上选择一个对象,但应在点击点上绘制十字准线

  • single-click paints crosshairs on the click point
  • double-click selects an object on the screen, but should not paint crosshairs on the click point

...任何人都可以想办法做到这一点?某种定时器设置可能吗?一个想法赞赏: - )

... can anyone think of a way to do this ? Some sort of timer set-up maybe ? An ideas appreciated :-)

< disclaimer> ...是的,我知道我提交一个最可恶的可用性/ UI虚假的pas。 < / disclaimer>

<disclaimer> ...and yes, I know I'm committing a most heinous usability / UI faux pas. </disclaimer>

EDIT#2:

由于定时器的延迟是疯狂的 - 我放弃这个解决方案,并使用中间点击选择,而不是双击...

Even though this works the delay due to the timer is maddening - I'm abandoning this solution, and using middle-click for selection instead of double-click...

编辑:

感谢 cgull - 这是什么我能够提出你的确认,没有简单的方法来做这个(请注意,如果我设置的定时器< 200多次赛车是看到之间的点击和定时器,但只要我设置为一个value> 200 things work just peachy):

Thanks cgull - this is what I was able to come up with given your confirmation that there's no easy way to do this (note that if I set the timer < 200 odd racing is seen between the click & the timer, but as long as I set this to a value > 200 things work just peachy) :

public void mouseClicked(MouseEvent e) {
    System.out.println( "Click at (" + e.getX() + ":" + e.getY() + ")" );
    if (e.getClickCount() == 2) {  
        System.out.println( "  and it's a double click!");
        wasDoubleClick = true;
    }else{
        Integer timerinterval = (Integer) 
          Toolkit.getDefaultToolkit().getDesktopProperty(
                      "awt.multiClickInterval");
        timer = new Timer(timerinterval.intValue(), new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if (wasDoubleClick) {
                    wasDoubleClick = false; // reset flag
                } else {
                    System.out.println( "  and it's a simple click!");
                }
            }    
        });
        timer.setRepeats(false);
        timer.start();
    }
}


推荐答案

您需要在MouseAdapter的覆盖mouseClicked()方法中设置一个计时器,以检测两次点击之间的时间间隔。可以通过查询
Toolkit.getDefaultToolkit()。getDesktopProperty(awt.multiClickInterval)找到默认间隔(以毫秒为单位)。如果在定时器到期之前检测到另一个鼠标点击,则您双击,否则一旦定时器到期,您可以处理一次点击。

Indeed you'll need to set up a Timer in your overridden mouseClicked() method of your MouseAdapter to detect the time interval between the two clicks. The default interval in ms can be found by querying Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"). If another mouse click is detected before the timer expires, then you have a double-click, else once the timer expires, you can process the single-click.

这篇关于Java:忽略单击双击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆