简体   繁体   English

JButton长按事件

[英]JButton long press event

I am developing Java desktop application in NetBeans platform. 我正在NetBeans平台上开发Java桌面应用程序。 I have several JFrame s and within these frames I have several JButton s. 我有几个JFrame ,在这些框架中我有几个JButton

My application will be run on touch panels like industrial PCs, Linux based panel PCs etc. So I will need to use long press event of a button. 我的应用程序将在工业PC,基于Linux的Panel PC等触摸面板上运行。因此,我需要使用按钮的长按事件。

How can I handle long press event of JButton ? 如何处理JButton长按事件 Click event is OK but I could not find any references or samples about long press/long click. 单击事件是可以的,但我找不到任何有关长按/长按的参考或示例。

If you decided for your implementation to use JButton, you should be aware that usually you don't use "click events" with them (although you, in theory, can use some sort of MouseListener combo to achieve this) - all AbstractButton subclasses have an ActionListener queue handling the default platform event of activating the button. 如果您决定让实现使用JButton,则应注意通常不会与它们一起使用“单击事件”(尽管从理论上讲,您可以使用某种MouseListener组合来实现此目的)-所有AbstractButton子类都具有一个ActionListener队列,处理激活按钮的默认平台事件。 You should thus focus on Actions instead of 'clicks' 因此,您应该专注于“操作”而不是“点击”

http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#abstractbutton http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#abstractbutton

http://docs.oracle.com/javase/7/docs/api/javax/swing/Action.html#buttonActions http://docs.oracle.com/javase/7/docs/api/javax/swing/Action.html#buttonActions

If you're sure you want to monitor for long press events on JButton objects anyway, add a timer to the ActionListener, eg by means of System.currentTimeMillis(), to check the time difference between actions and/or use MouseListener (all java.awt.Component subclasses has addMouseListener() defined) with mousePressed/mouseReleased event time measurement to get the time delta so that you can detect the length of the 'press'. 如果您确定要监视JButton对象上的长按事件,请向ActionListener添加一个计时器,例如通过System.currentTimeMillis(),以检查动作之间的时间差和/或使用MouseListener(所有java .awt.Component子类具有带有mousePressed / mouseReleased事件时间度量的addMouseListener()定义)以获取时间增量,以便您可以检测“ press”的长度。

This code worked for me. 这段代码对我有用。

abstract class MouseCustomAdapter extends MouseAdapter {
    private long mousePressedTime;
    private long delay = 1000;
    private Timer flashTimer;
    private Color originalForegroungColor;

    public MouseCustomAdapter() {}
    public MouseCustomAdapter(long delay) {
        this.delay = delay;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        mousePressedTime = e.getWhen();
        if(flashTimer != null)
            flashTimer.cancel();
        flashTimer = new Timer("flash timer");
        flashTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                originalForegroungColor = e.getComponent().getForeground();
                e.getComponent().setForeground(Color.LIGHT_GRAY);
            }
        }, delay);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        flashTimer.cancel();
        e.getComponent().setForeground(originalForegroungColor);
        if(e.getWhen() - mousePressedTime > delay)
            longActionPerformed(e);
        else
            shortActionPerformed(e);
    }

    public abstract void shortActionPerformed(MouseEvent e);
    public abstract void longActionPerformed(MouseEvent e);
}

Extends the adapter implementing shortActionPerformed and longActionPerformed. 扩展实现shortActionPerformed和longActionPerformed的适配器。 Es: Es:

    menuButton.addMouseListener(new MouseCustomAdapter() {
        @Override
        public void shortActionPerformed(MouseEvent e) {
            System.out.prinln("Pressed short");
        }

        @Override
        public void longActionPerformed(MouseEvent e) {
            System.out.prinln("Pressed long");
        }
    });

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

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