繁体   English   中英

事件侦听器和鼠标侦听器

[英]Event listeners and Mouse listeners

我想知道是否可以使用事件侦听器(而不是鼠标侦听器)检查JButton是否被双击。 考虑下面的代码;

public void actionPerformed(ActionEvent arg0){
    if (arg0.getClickCount() == 2){
        System.out.println("You Doubled clicked");
    }
}

我收到一条错误消息,指出getClickCount() is undefined for the type ActionEvent 鼠标的单击或双击是否不被视为事件? 思考。

你不能 如果不确定,请阅读文档。 方法OnClickCount在Action Event类中不存在,仅在MouseEvent类中可用。 如果需要,请编写自己的方法。

请参阅以下文档以供参考

http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionEvent.html

http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseEvent.html

您要使用MouseAdapter 它允许您不要使用不必要的方法( mouseDraggedmouseEntered等)使代码混乱。

public class MyClass extends MouseAdapter {
    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            // Double click
        } else {
           // Simple click
        }
    }     
}

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

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()”方法。
请参阅文档: ActionEvent API文档

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

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

然后,如果“ numClicks”等于“ 2”,则发生了鼠标双击,则可以将其设置回零,依此类推。

答案取决于。 您是否只想知道何时两次单击按钮或何时两次单击按钮?

通常不建议将MouseListener附加到按钮,因为可以通过多种方式触发按钮,包括以编程方式

您需要做的不仅是计算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");
            }
        }


    }

}

暂无
暂无

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

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