gpt4 book ai didi

java - 使用 JavaFX 进行拖放操作,是否可以让被拖动对象的幽灵跟随光标?

转载 作者:行者123 更新时间:2023-12-02 01:23:43 30 4
gpt4 key购买 nike

我一直在寻找使用 Java 进行拖放的示例,但他们总是使用通用鼠标光标,并附加一个框来指示正在拖动项目,而许多工具(甚至像 Firefox 这样的浏览器)则附加了一个幽灵将对象拖动到光标处以指示正在拖动的内容。这可以在 JavaFX 中完成吗?

最佳答案

您可以使用DragBoard.setDragView(...);设置拖动过程中显示的图像。

示例代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class DragViewExample extends Application {

@Override
public void start(Stage primaryStage) {
TextField tf = new TextField("Drag from here");
Label label = new Label("Drop here");
tf.setOnDragDetected(e -> {
Dragboard db = tf.startDragAndDrop(TransferMode.COPY);
db.setDragView(new Text(tf.getText()).snapshot(null, null), e.getX(), e.getY());
ClipboardContent cc = new ClipboardContent();
cc.putString(tf.getText());
db.setContent(cc);
});
label.setOnDragOver(e -> {
e.acceptTransferModes(TransferMode.COPY);
});
label.setOnDragDropped(e -> {
Dragboard db = e.getDragboard();
if (db.hasString()) {
label.setText(db.getString());
e.setDropCompleted(true);
} else {
e.setDropCompleted(false);
}
});

Scene scene = new Scene(new StackPane(new HBox(10, tf, label)), 350, 75);
primaryStage.setScene(scene);
primaryStage.show();
}

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

关于java - 使用 JavaFX 进行拖放操作,是否可以让被拖动对象的幽灵跟随光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29711190/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com