我有一個節點,我想實現拖放(此對象是源不是目標)。我也希望對象隨鼠標光標一起移動。我設法做到了這兩個,但不是在同一時間。如何在拖放操作中執行代碼?
看起來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