区分Java中的单击和双击

时间:2011-01-02 06:26:16

标签: java mouseevent double-click

我搜索论坛并查看此代码:

            public void mouseClicked(MouseEvent e) {
            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();
            }

        }

但是代码运行不正确(有时它打印出“并且只需单击一次!”2次。它应该打印出来“并且它是双击!”)。任何人都可以告诉我为什么吗?或者你能给我一些更好的方法吗? 谢谢!

2 个答案:

答案 0 :(得分:17)

  

有时打印出来“只需点击一下!” 2次 。它应该打印出来“并且它是双击!”)。

这很正常。如果在指定的时间间隔内单击两次,则仅发生双击。因此,如果您没有足够快地点击,有时会连续两次点击。

Integer timerinterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"); 

上面的代码行决定了双击的速度。

这里的价值在于我曾经做过同样的事情。不知道它是否比你的代码更好或更差:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class ClickListener extends MouseAdapter implements ActionListener
{
    private final static int clickInterval = (Integer)Toolkit.getDefaultToolkit().
        getDesktopProperty("awt.multiClickInterval");

    MouseEvent lastEvent;
    Timer timer;

    public ClickListener()
    {
        this(clickInterval);
    }

    public ClickListener(int delay)
    {
        timer = new Timer( delay, this);
    }

    public void mouseClicked (MouseEvent e)
    {
        if (e.getClickCount() > 2) return;

        lastEvent = e;

        if (timer.isRunning())
        {
            timer.stop();
            doubleClick( lastEvent );
        }
        else
        {
            timer.restart();
        }
    }

    public void actionPerformed(ActionEvent e)
    {
        timer.stop();
        singleClick( lastEvent );
    }

    public void singleClick(MouseEvent e) {}
    public void doubleClick(MouseEvent e) {}

    public static void main(String[] args)
    {
        JFrame frame = new JFrame( "Double Click Test" );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.addMouseListener( new ClickListener()
        {
            public void singleClick(MouseEvent e)
            {
                System.out.println("single");
            }

            public void doubleClick(MouseEvent e)
            {
                System.out.println("double");
            }
        });
        frame.setSize(200, 200);
        frame.setVisible(true);
     }
}

答案 1 :(得分:0)

 public void mouseClicked(MouseEvent evt) { 
   if (evt.getButton()==MouseEvent.BUTTON1){
    leftClick = true; clickCount = 0;
    if(evt.getClickCount() == 2) doubleClick=true;
    Integer timerinterval = (Integer)Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");

               timer = new Timer(timerinterval, new ActionListener() {
                public void actionPerformed(ActionEvent evt) {  
                    if(doubleClick){
                        System.out.println("double click.");
                        sb = new StringBuffer();
                        sb.append("Double Click");
                        clickCount++;
                        if(clickCount == 2){                               
                            clickCount=0;
                            doubleClick = false;
                        }

                    } else {

                        sb = new StringBuffer();
                        sb.append("Left Mouse");
                        System.out.println("single click.");                           
                    }
                }               
            });
            timer.setRepeats(false);
            timer.start();
            if(evt.getID()==MouseEvent.MOUSE_RELEASED) timer.stop();
}