Java中单击和双击的区别

发布于 2024-10-09 16:06:37 字数 1045 浏览 6 评论 0原文

我搜索论坛并看到以下代码:

            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次。它应该打印出“并且是双击!”)。有人能告诉我为什么吗?或者你能给我一些更好的方法来做到这一点吗? 谢谢你!

I search the forum and see this codes:

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

        }

but the code runs incorrectly(Sometime it prints out " and it's a single click!" 2 times . It should print out " and it's a double click!"). Can anybody show me why? or can you give me some better ways to do this?
Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻安 2024-10-16 16:06:37

有时它会打印出“and it is a single click!” 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);
     }
}

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

That is normal. A double click only happens if you click twice within the specified time interval. So sometimes if you don't click fast enough you will get two single clicks in a row.

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

The above line of code determines how fast the double click must be.

For what its worth here is some code I have used to do the same thing. Don't know if its any better or worse than the code you have:

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);
     }
}
对你再特殊 2024-10-16 16:06:37
 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();
}           
 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();
}           
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文