JavaFX 鼠标拖动事件未触发

JavaFX mouse drag events not firing

我几乎尝试了所有方法,但鼠标拖动事件没有触发,如下所述:

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

这是一个最小的例子,所以你可以试试看(我正在使用 Java 11 和 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);
    }
}

我想通过 setOnMouseDragReleasedbutton1gridPane 中获取 button2 的引用。有很多嵌套的窗格等,因为我想保持原来的项目布局结构。我这样做是因为我不确定这是否也是无法运行的原因。

提前致谢。

MOUSE_DRAG_RELEASED 在此节点上结束拖动时触发。例如

 centerBorderPane.setOnMouseDragReleased((event) -> {
        System.out.println("centerBorderPane drag released");
 });

应该在您拖动 button2 并且拖动结束于 centerBorderPane 时触发。

要在将鼠标拖过 button1 时触发事件,请使用 button1.setOnMouseDragged
如果您想将鼠标事件从父项传播到其子项,请参阅

我最终使用 node.fireEvent(event) 手动触发了从 centerBorderPanegridPane 的事件。还实现了一个辅助函数,其中returns右子节点:

  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();
  }

不要忘记消费事件,这样你就不会陷入死循环。