简体   繁体   English

在java中识别双击

[英]identifying double click in java

我想知道在组件中双击鼠标时我们如何执行操作。

public void mouseClicked(MouseEvent event)
{
  if (event.getClickCount() == 2 && event.getButton() == MouseEvent.BUTTON1) {
    System.out.println("double clicked");
  }
}

Assuming you mean in Swing, assign a MouseListener to your Component:假设您的意思是在 Swing 中,为您的组件分配一个 MouseListener:

addMouseListener(new MouseAdapter(){
    @Override
    public void mouseClicked(MouseEvent e){
        if(e.getClickCount()==2){
            // your code here
        }
    }
});

Reference:参考:

The e.getClickCount()==2 is not enough if you want to allow your users to do multiple double clicks in a short delay.如果您想让您的用户在短时间内进行多次双击,则e.getClickCount()==2是不够的。 You are limited by the desktop configuration.您受到桌面配置的限制。 You can get it by looking the result of Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");您可以通过查看Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");的结果来获取它Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");

A good way to bypass the problem is not to use the getClickCount() check but to use a Timer where you can choose the interval max between your clicks and to handle by oneself the count (very simple).绕过该问题的一个好方法是不使用getClickCount()检查,而是使用Timer ,您可以在其中选择单击之间的最大间隔并自行处理计数(非常简单)。

The code associated :相关代码:

boolean isAlreadyOneClick;

@Override
public void mouseClicked(MouseEvent mouseEvent) {
    if (isAlreadyOneClick) {
        System.out.println("double click");
        isAlreadyOneClick = false;
    } else {
        isAlreadyOneClick = true;
        Timer t = new Timer("doubleclickTimer", false);
        t.schedule(new TimerTask() {

            @Override
            public void run() {
                isAlreadyOneClick = false;
            }
        }, 500);
    }
}

Tested with Win Xp OS and perfect.用 Win Xp OS 测试过,完美。

My problem is that I have to respond one way if the user single clicks, another if they click more than one time (my Swing VM seems to be able to count up to four clicks when I click multiple times).我的问题是,如果用户单击一次,我必须以一种方式响应,如果用户单击多次,则必须以另一种方式响应(当我多次单击时,我的 Swing VM 似乎最多可以计算四次点击)。 When I ran the example above, it seemed to count a triple click as a single one.当我运行上面的示例时,它似乎将三次单击视为一次单击。 So, here is my rewrite.所以,这是我的重写。 Basically, I just have a scheduled task that waits until the dust clears and then checks the number of clicks registered.基本上,我只是有一个计划任务,它会等待灰尘清除,然后检查注册的点击次数。 The 400 ms wait seems to work best for me. 400 毫秒的等待似乎最适合我。

JButton jButton = new JButton("Click Me!");
jButton.addMouseListener(new MouseAdapter() {
    private int eventCnt = 0;
    java.util.Timer timer = new java.util.Timer("doubleClickTimer", false);

    @Override
    public void mouseClicked(final MouseEvent e) {
        eventCnt = e.getClickCount();
        if ( e.getClickCount() == 1 ) {
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    if ( eventCnt == 1 ) {
                        System.err.println( "You did a single click.");
                    } else if ( eventCnt > 1 ) {
                        System.err.println("you clicked " + eventCnt + " times.");
                    }
                    eventCnt = 0;
                }
            }, 400);
        }
    }
});

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

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