如何实现javafx鼠标事件“push and hold”? [英] how to achieve javafx mouse event "push and hold"?

查看:110
本文介绍了如何实现javafx鼠标事件“push and hold”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个javafx项目,我需要类似于触摸事件推送的东西,但我需要它作为一个鼠标事件,因为我遇到了与Linux和javafx的联系,我已经尝试过触摸事件,但Ubuntu不会回应触摸事件。那么关于如何实现事件或如何在Linux上激活javafx的触摸事件的任何想法?

解决方案

只需使用 PauseTransition 作为hold的计时器。如果按下鼠标,启动它,如果它被释放或拖动,请停止。

  import javafx.animation.PauseTransition; 
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class MousePressAndHoldTest extends Application {

@Override
public void start(Stage primaryStage){
Pane root = new Pane();

addPressAndHoldHandler(root,Duration.seconds(1),
event - > System.out.println(按住));


primaryStage.setScene(new Scene(root,400,400));
primaryStage.show();
}

private void addPressAndHoldHandler(Node node,Duration holdTime,
EventHandler< MouseEvent> handler){

class Wrapper< T> {T内容; }
Wrapper< MouseEvent> eventWrapper = new Wrapper<>();

PauseTransition holdTimer = new PauseTransition(holdTime);
holdTimer.setOnFinished(event - > handler.handle(eventWrapper.content));


node.addEventHandler(MouseEvent.MOUSE_PRESSED,event - > {
eventWrapper.content = event;
holdTimer.playFromStart();
} );
node.addEventHandler(MouseEvent.MOUSE_RELEASED,event - > holdTimer.stop());
node.addEventHandler(MouseEvent.DRAG_DETECTED,event - > holdTimer.stop());
}


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


I am developing a javafx project and I need something similar to touch event push and hold,but i need it as a mouse event because i am having trouble in touch with Linux and javafx, and I have tried the touch events but Ubuntu won't respond to touch events. So any ideas about how to achieve the event or how to activate the touch event of javafx on Linux ?

解决方案

Just use a PauseTransition as a timer for the "hold". Start it if the mouse is pressed, stop it if it's released or dragged.

import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class MousePressAndHoldTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        Pane root = new Pane();

        addPressAndHoldHandler(root, Duration.seconds(1), 
                event -> System.out.println("Press and hold"));


        primaryStage.setScene(new Scene(root, 400, 400));
        primaryStage.show();
    }

    private void addPressAndHoldHandler(Node node, Duration holdTime, 
            EventHandler<MouseEvent> handler) {

        class Wrapper<T> { T content ; }
        Wrapper<MouseEvent> eventWrapper = new Wrapper<>();

        PauseTransition holdTimer = new PauseTransition(holdTime);
        holdTimer.setOnFinished(event -> handler.handle(eventWrapper.content));


        node.addEventHandler(MouseEvent.MOUSE_PRESSED, event -> {
            eventWrapper.content = event ;
            holdTimer.playFromStart();
        });
        node.addEventHandler(MouseEvent.MOUSE_RELEASED, event -> holdTimer.stop());
        node.addEventHandler(MouseEvent.DRAG_DETECTED, event -> holdTimer.stop());
    }


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

这篇关于如何实现javafx鼠标事件“push and hold”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆