JavaFX鼠标拖动事件未触发 [英] JavaFX mouse drag events not firing

查看:90
本文介绍了JavaFX鼠标拖动事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎尝试了所有操作,但没有触发鼠标拖动事件,如此处所述:

I tried almost everything, but the mouse drag events are not firing, like explained here:

https://openjfx.io/javadoc/11/javafx.graphics/javafx/scene/input/MouseDragEvent.html

这是一个最小的示例,因此您可以尝试一下(我正在将Java 11与JavaFX 11.0.2一起使用):

Here is a minimal example, so you can try it out (I am using Java 11 with JavaFX 11.0.2):

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.stage.Stage;


public class Main extends Application {

    private double mouseClickPositionX, mouseClickPositionY, currentRelativePositionX, currentRelativePositionY;

    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.setTitle("Hello World");

        BorderPane mainBorderPane = new BorderPane();
        BorderPane centerBorderPane = new BorderPane();
        FlowPane flowPane = new FlowPane();
        GridPane gridPane = new GridPane();
        Button button1 = new Button("button1");
        gridPane.add(button1, 0, 0);
        flowPane.getChildren().add(gridPane);
        centerBorderPane.setCenter(flowPane);
        HBox hbox = new HBox();
        TilePane tilePane = new TilePane();
        Button button2 = new Button("button2");
        tilePane.getChildren().add(button2);
        hbox.getChildren().add(tilePane);
        mainBorderPane.setCenter(centerBorderPane);
        centerBorderPane.setBottom(hbox);

        // button2 event handlers

        button2.setOnMousePressed(event -> {
            mouseClickPositionX = event.getSceneX();
            mouseClickPositionY = event.getSceneY();
            currentRelativePositionX = button2.getTranslateX();
            currentRelativePositionY = button2.getTranslateY();
            button2.setMouseTransparent(true);
        });

        button2.setOnMouseDragged(event -> {
            button2.setTranslateX(currentRelativePositionX + (event.getSceneX() - mouseClickPositionX));
            button2.setTranslateY(currentRelativePositionY + (event.getSceneY() - mouseClickPositionY));
        });

        button2.setOnDragDetected(event -> {
            button2.startFullDrag();
        });

        button2.setOnMouseReleased((event) -> {
            button2.setMouseTransparent(false);
        });

        // button1 event handlers

        button1.setOnMouseDragReleased((event) -> {
            System.out.println("it works in button1");
        });

        // gridPane event handlers

        gridPane.setOnMouseDragReleased((event) -> {
            System.out.println("it works in gridPane");
        });

        primaryStage.setScene(new Scene(mainBorderPane, 300, 275));
        primaryStage.show();
    }


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

我想通过 setOnMouseDragReleased button1 gridPane 中获取 button2 的引用.有很多嵌套的窗格等,因为我想维护原始的项目布局结构.我这样做是因为我不确定这是否也可能是无法正常运行的原因.

I want to get the reference of button2 either in button1 or in gridPane via setOnMouseDragReleased. There are many nested panes etc. because I wanted to maintain the original project layout structure. I did this because I am not sure if this also can be a reason for the non functioning.

谢谢.

推荐答案

我最终使用 node触发了从 centerBorderPane gridPane 的事件.fireEvent(event).还实现了一个辅助函数,该函数返回正确的子节点:

I've ended up manually triggering the events from centerBorderPane to gridPane, using node.fireEvent(event). Also implemented a helper function, which returns the right child node:

  private Optional<Node> findNode(Pane pane, double x, double y) {
    return pane.getChildren().stream().filter(n -> {
      Point2D point = n.sceneToLocal(x, y);
      return n.contains(point.getX(), point.getY());
    }).findAny();
  }

别忘了消耗事件,因此您不会陷入无限循环.

Don't forget to consume the events, so you won't get into an infinite loop.

这篇关于JavaFX鼠标拖动事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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