java - 如何在 JavaFX 中检测鼠标单击?

标签 java javafx mouse

所以我开始在我的应用程序中为不同的功能设置按键,我想知道是否有一种方法可以设置鼠标右键/左键单击来执行某些操作?我在 KeyCode.java 中找不到任何鼠标事件 我不知道如何应用它。

这是我的代码:

public class Exercise_31 extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        PendulumPane pendulumPane = new PendulumPane(500, 500);

        Scene scene = new Scene(pendulumPane);
        primaryStage.setTitle("Pendulum Animation");
        primaryStage.setScene(scene);
        pendulumPane.play();
        primaryStage.show();


        scene.setOnKeyPressed(e-> {
            switch (e.getCode()) {
                case UP: pendulumPane.increase(); break;
                case DOWN: pendulumPane.decrease(); break;
                case LEFTMOUSECLICK?  
            }
        });


    }

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

    private class PendulumPane extends Pane {

        private double w = 400;
        private double h;
        PathTransition bPath;

        Circle topC;
        Circle lowerC;
        Line line;
        Arc arc;

        PendulumPane(double width, double height) {

            w = width;
            h = height;
            setPrefWidth(w);
            setPrefHeight(h);
            arc = new Arc(w / 2, h * 0.8, w * 0.15, w * 0.15, 180, 180);
            arc.setFill(Color.TRANSPARENT);
            arc.setStroke(Color.BLACK);

            lowerC = new Circle(arc.getCenterX() - arc.getRadiusX(), arc.getCenterY(), 10);
            topC = new Circle(arc.getCenterX(), arc.getCenterY() - h / 2, lowerC.getRadius() / 2);
            arc = new Arc(topC.getCenterX(), topC.getCenterY(), w / 2, h / 2, 240, 60);
            line = new Line(
                    topC.getCenterX(), topC.getCenterY(),
                    lowerC.getCenterX(), lowerC.getCenterY());

            line.endXProperty().bind(lowerC.translateXProperty().add(lowerC.getCenterX()));
            line.endYProperty().bind(lowerC.translateYProperty().add(lowerC.getCenterY()));
            bPath = new PathTransition();
            bPath.setDuration(Duration.millis(4000));
            bPath.setPath(arc);
            bPath.setNode(lowerC);
            bPath.setOrientation(PathTransition.OrientationType.NONE);
            bPath.setCycleCount(PathTransition.INDEFINITE);
            bPath.setAutoReverse(true);

            getChildren().addAll(lowerC, topC,line);

        }

        public void play() {
            bPath.play();
        }


        public void increase() {
            bPath.setRate(bPath.getCurrentRate() + 1);
        }

        public void decrease() {
            bPath.setRate(bPath.getCurrentRate() - 1);
        }

        public void stop() {
            bPath.setRate(bPath.getCurrentRate() * 0);
        }

        public void start() {
            bPath.setRate(bPath.getCurrentRate() + 1);
        }
    }
}

我想要完成的是右键单击鼠标按钮停止移动并单击左键再次启动它。我该怎么做呢?谢谢。

最佳答案

您的事件是一个KeyEvent。如果您为鼠标定义了一个监听器(例如 setOnMouseReleased),您将获得一个 MouseEvent,并且在该监听器上,您可以使用函数来检查是否按下了某些键,这些键通常与鼠标点击:

event.isAltDown();
event.isControlDown();
event.isMetaDown();

如果您需要更多东西,您必须自己实现。 请参阅 JavaDoc MouseEvent .

关于java - 如何在 JavaFX 中检测鼠标单击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34337091/

相关文章:

java - Apache Solr 无法存储嵌套 JSON 数据

java - 如何更改 JavaFX 中表格的颜色?

javascript - 如何传递 webengine.executeScript() 使用的 javascript 对象

java - 创建 spark 客户端失败 : Hive on spark exception

java - request.getparameter 没有给出字符串数组的正确答案

java - 为什么我的 JavaFX 窗口看起来到处都不一样?

cocoa - Cocoa 中的全局鼠标移动事件

javascript - 没有 Jquery 的 Canvas 上的鼠标坐标

java - 调用 repaint() 而不丢失先前的图形

java - SQL Developer,Java 1.7.0_07 错误