将 JavaFX 与拖放一起使用,是否可能使被拖动对象的幻影跟随光标?
Using JavaFX with drag and drop, is it possible to have a ghost of the dragged object follow the cursor?
我一直在寻找使用 Java 进行拖放的示例,但他们总是使用带有附加框的通用鼠标光标来指示正在拖动的项目,而许多工具(甚至是 Firefox 等浏览器)而是将拖动对象的重影附加到光标以指示正在拖动的内容。这可以在 JavaFX 中完成吗?
是的,您可以set a drag view(任意图像)进行拖放操作。
您可以使用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 进行拖放的示例,但他们总是使用带有附加框的通用鼠标光标来指示正在拖动的项目,而许多工具(甚至是 Firefox 等浏览器)而是将拖动对象的重影附加到光标以指示正在拖动的内容。这可以在 JavaFX 中完成吗?
是的,您可以set a drag view(任意图像)进行拖放操作。
您可以使用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);
}
}