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

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

问题描述

任何人都可以想到一个很好的方法来忽略Java中双击的单击?



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




  • 单击点击点击十字准线

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



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



<免责声明... ...是的,我知道我犯了一个最可恶的可用性/ UI人造皮肤。 < /免责声明>



编辑#2:



由于计时器的延迟令人生闷 - 我放弃了这个解决方案,并使用中间点击进行选择,而不是双击...



编辑:



感谢 cgull - 这是什么我能够提出你的确认,没有简单的方法来做到这一点(请注意,如果我设置了定时器< 200奇怪的赛车在点击和定时器之间,但只要我把它设置为价值> 200件事情只是peachy):

  public void mouseClicked(MouseEvent e){
System.out.println (点击(+ e.getX()+:+ e.getY()+));
if(e.getClickCount()== 2){
System.out.println(and it is a double click!);
wasDoubleClick = true;
} else {
整数timerinterval =(整数)
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 a a click click!);
}
}
});
timer.setRepeats(false);
timer.start();
}
}


解决方案

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


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> ...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...

EDIT:

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();
    }
}

解决方案

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天全站免登陆