Java 事件侦听器和鼠标侦听器

Java 事件侦听器和鼠标侦听器,java,swing,awt,actionlistener,mouse-listeners,Java,Swing,Awt,Actionlistener,Mouse Listeners,我想知道是否可以使用事件监听器而不是鼠标监听器来检查是否双击了JButton。考虑下面的代码; public void actionPerformed(ActionEvent arg0){ if (arg0.getClickCount() == 2){ System.out.println("You Doubled clicked"); } } 我收到一个错误,表示ActionEvent类型的getClickCount()未定义。鼠标的点击或双击是否也被视为事件

我想知道是否可以使用事件监听器而不是鼠标监听器来检查是否双击了JButton。考虑下面的代码;
public void actionPerformed(ActionEvent arg0){
    if (arg0.getClickCount() == 2){
        System.out.println("You Doubled clicked");
    }
}

我收到一个错误,表示ActionEvent类型的
getClickCount()未定义。鼠标的点击或双击是否也被视为事件?思想。

你不能。如果不确定,请阅读文档。方法OnClickCount在操作事件类中不存在,它仅在MouseeEvent类中可用。如果你想,那么写你自己的方法

请参阅以下文档以供参考


您想使用
鼠标头捕捉器
。它允许您不使用不必要的方法(mouseDragged
mouseEntered
等)使代码杂乱无章

或者,如果您的类已经扩展了另一个类,请尝试以下代码:

public class MyClass extends MyBaseClass {
    private MouseAdapter ma;

    public MyClass () {
        final MyClass that = this;
        ma = new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                that.myMouseClickedHandler(e);
            }
        };
    }

    public void myMouseClickedHandler(MouseEvent e) {
        if (e.getClickCount() == 2) {
            // Double click
        } else {
           // Simple click
        }        
    }
 }

ActionEvent没有“getClickCount()”方法。
请参阅文档:

您可以在actionPerformed方法中定义变量“numClicks”:

public void actionPerformed(ActionEvent e) {
     numClicks++;

然后,如果“numClicks”等于“2”,则双击鼠标,然后可以将其设置回零,等等。

答案视情况而定。你只想知道按钮被“点击”两次还是被“按下”两次

通常不建议在按钮上附加
鼠标侦听器
,因为按钮可以通过多种方式触发,包括编程方式

您需要能够做的不仅仅是计算调用
actionPerformed
的次数,还需要知道两次单击之间的时间间隔

您可以记录最后一次单击时间,并将其与当前时间进行比较,然后以这种方式进行确定,或者您可以简单地使用
javax.swing.Timer
来为您执行此操作

下面的示例还检查
ActionEvent
的最后一个源是否与当前源相同,如果不是,则重置计数器

这也允许鼠标点击、按键和编程触发器

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TimerButton {

     public static void main(String[] args) {
        new TimerButton();
    }

    public TimerButton() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JButton btn = new JButton("Testing");
                btn.addActionListener(new ActionHandler());

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(btn);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ActionHandler implements ActionListener {

        private Timer timer;
        private int count;
        private ActionEvent lastEvent;

        public ActionHandler() {
            timer = new Timer(250, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Tick " + count);
                    if (count == 2) {
                        doubleActionPerformed();
                    }
                    count = 0;
                }
            });
            timer.setRepeats(false);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (lastEvent != null && lastEvent.getSource() != e.getSource()) {
                System.out.println("Reset");
                count = 0;
            }
            lastEvent = e;
            ((JButton)e.getSource()).setText("Testing");
            count++;
            System.out.println(count);
            timer.restart();
        }

        protected void doubleActionPerformed() {
            Object source = lastEvent.getSource();
            if (source instanceof JButton) {
                ((JButton)source).setText("Double tapped");
            }
        }


    }

}

关于如何创建自己的方法来检查JButton是连续单击还是双击,有什么建议吗?如果这是一个天真的问题,我道歉。我对java还是很陌生。你为什么要重新发明轮子?使用此方法btn.addMouseListener(new java.awt.event.MouseAdapter(){public void mouseClicked(java.awt.event.MouseEvent evt){if(evt.getClickCount()==2){//Do your want here}});虽然这种方法更好地满足了用户的需求,但它留下了一个问题,即如何确定用户是否不只是单击一次,离开一段时间,然后再次单击。答案取决于OP是否只想知道鼠标单击或者是否想继续支持正常交互(例如[enter]或[space])我想知道这个按钮是否被按了两次。是的,这会告诉你它是否被“按”了两次,不管是用鼠标、键盘还是编程
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TimerButton {

     public static void main(String[] args) {
        new TimerButton();
    }

    public TimerButton() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JButton btn = new JButton("Testing");
                btn.addActionListener(new ActionHandler());

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(btn);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ActionHandler implements ActionListener {

        private Timer timer;
        private int count;
        private ActionEvent lastEvent;

        public ActionHandler() {
            timer = new Timer(250, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Tick " + count);
                    if (count == 2) {
                        doubleActionPerformed();
                    }
                    count = 0;
                }
            });
            timer.setRepeats(false);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (lastEvent != null && lastEvent.getSource() != e.getSource()) {
                System.out.println("Reset");
                count = 0;
            }
            lastEvent = e;
            ((JButton)e.getSource()).setText("Testing");
            count++;
            System.out.println(count);
            timer.restart();
        }

        protected void doubleActionPerformed() {
            Object source = lastEvent.getSource();
            if (source instanceof JButton) {
                ((JButton)source).setText("Double tapped");
            }
        }


    }

}