onDragOver没有发生(在JavaFX中)

时间:2016-10-16 19:41:21

标签: java javafx drag-and-drop

为什么在以下示例中不会发生onDragOver事件?

如何实现最简单的拖动行为,即没有剪贴板的东西?

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.input.DragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Pane;
import javafx.scene.paint.*;
import javafx.stage.Stage;

public class DragTry extends Application {

   private Point2D dragVector = null;

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

      Pane root = new Pane();

      Pane node = new Pane();
      node.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, javafx.geometry.Insets.EMPTY)));
      node.setPrefWidth(100);
      node.setPrefHeight(50);
      node.setTranslateX(200);
      node.setTranslateY(200);

      node.setOnDragDetected(new EventHandler<MouseEvent>() {
         @Override
         public void handle(MouseEvent event) {
            System.out.println("odDragDetected");

            double x = event.getX();
            double y = event.getY();
            x -= node.getTranslateX();
            y -= node.getTranslateY();
            dragVector = new Point2D(x, y);
            node.startFullDrag();
         }
      });

      node.setOnDragOver(new EventHandler<DragEvent>() {
         @Override
         public void handle(DragEvent event) {
            System.out.println("onDragOver");

            double x = event.getX();
            double y = event.getY();
            x += dragVector.getX();
            y += dragVector.getY();
            node.setTranslateX(x);
            node.setTranslateY(y);
         }
      });

      node.setOnDragDone(new EventHandler<DragEvent>() {
         @Override
         public void handle(DragEvent event) {
            dragVector = null;
         }
      });

      root.getChildren().add(node);

      Scene scene = new Scene(root, 800, 600);

      primaryStage.setScene(scene);
      primaryStage.setTitle("DragTry");
      primaryStage.show();
   }


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

2 个答案:

答案 0 :(得分:1)

首先,您需要致电CommonModule, FormsModule, HttpModule,而不是startDragAndDropdocumentation for MouseEvent中描述了不同的拖动模式。此外,拖放API的一个令人讨厌的部分是除非您向拖拽板添加内容,否则不会激活拖动:

startFullDrag

答案 1 :(得分:1)

onDragXYZ事件用于剪贴板拖动事件。对于其他类型的拖放功能,您需要使用onMouseDragXYZ事件。更重要的是,如果您正在修改,最好不要使用Node自己的坐标系中的坐标。

node.setOnDragDetected(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent event) {
        System.out.println("odDragDetected");

        double x = event.getX();
        double y = event.getY();
        dragVector = node.localToParent(x, y).subtract(node.getTranslateX(), node.getTranslateY());
        node.startFullDrag();
    }
});

node.setOnMouseDragOver(new EventHandler<MouseDragEvent>() {
    @Override
    public void handle(MouseDragEvent event) {
        System.out.println("onDragOver");

        Point2D parentCoords = node.localToParent(event.getX(), event.getY());

        node.setTranslateX(parentCoords.getX() -dragVector.getX());
        node.setTranslateY(parentCoords.getY() -dragVector.getY());
    }
});

node.setOnMouseReleased(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent event) {
        dragVector = null;
    }
});

但是,由于手势中只涉及单个节点,因此无需使用拖动事件。您只需使用onMousePressed代替onDragDetectedonMouseDragged代替onMouseDragOver