Java:忽略单击双击?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/548180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:49:04  来源:igfitidea点击:

Java : ignore single click on double click?

javaeventsclickmousedouble-click

提问by Dave Carpeneto

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

任何人都可以想出一种好方法来忽略 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 notpaint 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:

编辑#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) :

谢谢cgull- 这是我能够想出的,因为您确认没有简单的方法可以做到这一点(请注意,如果我将计时器设置为 <200将其设置为一个值 > 200 事情就很好了):

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

采纳答案by cgull

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.

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

回答by Bat0u89

Actually I think there is a simpler solution (use InputEvent's getWhen() method):

其实我认为有一个更简单的解决方案(使用 InputEvent 的 getWhen() 方法):

class DCListener extends MouseAdapter{

    private long maxTimeBetweenClicks; // in millis
    private long firstClickTime=0;
    private Runnable eventHandler;

    public DCListener(long maxTimeBetweenClicks,Runnable eventHandler){
        this.maxTimeBetweenClicks=maxTimeBetweenClicks;
        this.eventHandler=eventHandler;
    }

    public void mouseClicked(MouseEvent e){

        if((e.getWhen()-firstClickTime)<=maxTimeBetweenClicks){
            firstClickTime=0; // 3 clicks are not 2 double clicks
            eventHandler.run();
        }else{
            firstClickTime=e.getWhen();
        }

    }
}

回答by rendon

An alternative solution:

另一种解决方案:

I figured out this before I found the solution in this question. The idea is the same, use a timer, although more complicated :).

在我找到这个问题的解决方案之前,我想出了这一点。想法是一样的,使用计时器,虽然更复杂:)。

Use SwingWorker:

使用 SwingWorker

class BackgroundClickHandler extends SwingWorker<Integer, Integer> {
  @Override
  protected Integer doInBackground() throws Exception {
    Thread.sleep(200);
    // Do what you want with single click
    return 0;
  }

}

In mouseClicked()method you can do something like this:

mouseClicked()方法中,您可以执行以下操作:

 if (event.getClickCount() == 1) {
  // We create a new instance everytime since
  // the execute() method is designed to be executed once
  clickHandler = new BackgroundClickHandler();

  try {
    clickHandler.execute();
  } catch(Exception e) {
    writer.println("Here!");
  }
}

if (event.getClickCount() == 2) {
  clickHandler.cancel(true);

  //Do what you want with double click
}

I hope it be useful.

我希望它有用。

回答by OscarRyz

Have you tried implementing the MouseListener interface already?

您是否尝试过实现 MouseListener 接口?

I think MouseEvent has a click count method ( or property ) to know that.

我认为 MouseEvent 有一个点击计数方法(或属性)来知道这一点。

I bet you have gone through that already, so what is the problem you're facing there?

我敢打赌你已经经历过那个,那么你在那里面临的问题是什么?

Probably what you can do is to code the time interval elapsed between a single and a double click with a thread or something.

可能你可以做的是用线程或其他东西对单击和双击之间经过的时间间隔进行编码。

So a single click will only be valid if another click is not issued in let's say 300 ms. ( something configurable )

因此,只有在 300 毫秒内未发出另一次点击时,一次点击才有效。(可配置的东西)

The idea is:

这个想法是:

public void listen for the single click() 
    if (  in x time there has not been another click  ) 
    then 
        we have a single click
        proceed with your single click handling
    else 
        we have a double click
       proceed with your double click handling

Or something like that.

或类似的东西。