区分Java中的单击和双击

前端之家收集整理的这篇文章主要介绍了区分Java中的单击和双击前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在论坛上搜索并看到这段代码
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次,它应该打印出来,这是双击!”).有人能告诉我为什么吗或者你能给我一些更好的方法来做到这一点吗?
谢谢!

解决方法

Sometime it prints out ” and it’s a single click!” 2 times . It should print out ” and it’s a double click!”).

这是正常现象只有在指定的时间间隔内单击两次,才会发生双击.所以有时如果你没有点击足够快,你会得到两次单次点击.

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

猜你在找的Java相关文章