我有一个节点,我想实现拖放(此对象是源不是目标)。我也希望对象随鼠标光标一起移动。我设法做到了这两个,但不是在同一时间。如何在拖放操作中执行代码?
看起来setOnDragDetected
和setOnMouseDragged
不能很好地协同工作。考虑一个节点具有以下处理:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Example extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Rectangle rect = new Rectangle(20, 20);
rect.setOnMousePressed(e -> System.out.println("Pressed"));
rect.setOnMouseDragged(e -> System.out.println("Dragged"));
rect.setOnDragDetected(e -> {
System.out.println("Detected");
ClipboardContent content = new ClipboardContent();
content.putString("something");
Dragboard db = rect.startDragAndDrop(TransferMode.ANY);
db.setContent(content);
});
Group subGroup = new Group(rect);
Scene scene = new Scene(subGroup, 100, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Example.launch(args);
}
}
现在点击鼠标的节点上,然后移动鼠标。这是输出:
Pressed
Dragged
Dragged
Dragged
Dragged
Dragged
Dragged
Detected
一旦检测到拖动,MouseDragged
处理程序停止。
我如何实现我所描述的?我注意到的一件事可能是我可以使用onDragOver
作为父项,但我希望行为在节点中,因为那就是它应该在的位置。
我根本没有观察到这种行为;检测到拖动手势后,onMouseDragged处理程序继续进行报告。你应该创建一个[MCVE]并且[编辑]你的问题来包含它。 –
@James_D对不起。这里是代码。它与我想的拖板有关。 – Mark