Class 在javafx中创建长时间单击或按下按钮的方法

Class 在javafx中创建长时间单击或按下按钮的方法,class,javafx,click,long-integer,pressed,Class,Javafx,Click,Long Integer,Pressed,在我的程序中,我需要在长按/单击按钮时执行操作。因此,我决定创建一个方法,该方法在按钮的参数中获取按钮,并在按钮单击时间超过1秒时返回布尔值: public class EventOperations { JFXButton button; boolean result = false; // Button long click public EventOperations(JFXButton btn) { button = btn; } public void isLongPre

在我的程序中,我需要在长按/单击按钮时执行操作。因此,我决定创建一个方法,该方法在按钮的参数中获取按钮,并在按钮单击时间超过1秒时返回布尔值:

public class EventOperations {

JFXButton button;
boolean result = false;

// Button long click
public EventOperations(JFXButton btn) {
    button = btn;
}

public void isLongPressed() {

    final AnimationTimer timer = new AnimationTimer() {

    private long lastUpdate = 0;

    @Override
    public void handle(long time) {
        if (this.lastUpdate > 2000000000) {
            result = true;
            System.out.println("PRESSED !!!!!!!!!");
        }
        this.lastUpdate = time;
    }
    };

    button.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {
            if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) {
                timer.start();
            } else {
                timer.stop();
            }
        }
    });

}

public boolean getIsPressed() {
    return result;
}
但每次我快速点击按钮时,都会显示几个按钮被按下!!!所以它不起作用。20000000000的数字就是一个例子。 在我的主java方法中,我能做些什么来获取布尔值,如果它被按下,调用一个函数来做些什么


编辑!很好,谢谢你

使用开始和结束时间怎么样:

button.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {

        long startTime;

        @Override
        public void handle(MouseEvent event) {
            if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) {
                startTime = System.currentTimeMillis();
            } else if (event.getEventType().equals(MouseEvent.MOUSE_RELEASED)) {
                if (System.currentTimeMillis() - startTime > 2 * 1000) {
                    System.out.println("Pressed for at least 2 seconds (" + (System.currentTimeMillis() - startTime) + " milliseconds)");
                } else
                    System.out.println("Pressed for " + (System.currentTimeMillis() - startTime) + " milliseconds");
            }
        }
    });
中句柄方法的时间参数是当前帧的时间戳,单位为纳秒。因此,您不应该将lastUpdate设置为该值,并将其与所需的鼠标保持时间间隔进行比较

您应该在参数lastUpdate中单击鼠标时记录时间戳,句柄事件应该检查当前时间和lastUpdate之间的差异,以查看它是否大于鼠标保持间隔

生成的代码如下所示:

public void isLongPressed() {

    final AnimationTimer timer = new AnimationTimer() {

    private long lastUpdate = 0;

    @Override
    public void handle(long time) {
        if (Instant.now() - lastUpdate > 2000000000) {
            result = true;
            System.out.println("PRESSED !!!!!!!!!");
        }
        stop();
    }

    @Override
    public void start() {
        super.start();
        lastUpdate = Instant.now(); // Assuming you are using Java 9
    }
    };

    button.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {
            if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) {
                timer.start();
            } else {
                timer.stop();
            }
        }
    });
}
public void isLongPressed() {

    final AnimationTimer timer = new AnimationTimer() {

    private long lastUpdate = 0;

    @Override
    public void handle(long time) {
        if (Instant.now() - lastUpdate > 2000000000) {
            result = true;
            System.out.println("PRESSED !!!!!!!!!");
        }
        stop();
    }

    @Override
    public void start() {
        super.start();
        lastUpdate = Instant.now(); // Assuming you are using Java 9
    }
    };

    button.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {
            if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) {
                timer.start();
            } else {
                timer.stop();
            }
        }
    });
}