效果
![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/b39891a3275e36211413b08a0ab8b78a.gif)
代码
package cn.juhe.zjsb.test;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.Serializable;
public class DragAndDropPersonExample extends Application {
public DataFormat dataFormat = new DataFormat("data/person");
VBox getVBox() {
VBox vBox = new VBox(10);
vBox.setPrefWidth(300);
vBox.setPrefHeight(400);
vBox.setStyle("-fx-border-color: #ff0000");
return vBox;
}
@Override
public void start(Stage primaryStage) throws Exception {
AnchorPane anchorPane = new AnchorPane();
Scene scene = new Scene(anchorPane, 900, 900);
VBox vBox = getVBox();
VBox vBoxView = getVBox();
Person person = new Person("小倩老师", "18", "file:/Users/java0904/zhengjianshibie/src/main/resources/cn/juhe/zjsb/img/laoshi.jpeg");
Button infoBtn = new Button("个人详情");
infoBtn.prefWidthProperty().bind(vBox.widthProperty());
TextField nameField = new TextField();
TextField ageField = new TextField();
ImageView imageView = new ImageView();
TextField nameFieldView = new TextField(person.getName());
TextField ageFieldView = new TextField(person.getAge());
ImageView imageViewView = new ImageView(new Image(person.getPhoto()));
imageView.setPreserveRatio(true);
imageView.setFitWidth(300);
imageViewView.setPreserveRatio(true);
imageViewView.setFitWidth(300);
vBoxView.getChildren().addAll(nameFieldView,ageFieldView,imageViewView);
vBox.getChildren().addAll(infoBtn,nameField,ageField,imageView);
Button personBtn = new Button(person.getName());
personBtn.setOnDragDetected(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
Dragboard dragboard = personBtn.startDragAndDrop(TransferMode.MOVE);
ClipboardContent clipboardContent = new ClipboardContent();
clipboardContent.put(dataFormat,person);
anchorPane.getChildren().add(vBoxView);
WritableImage writableImage = new WritableImage(200,300);
vBoxView.snapshot(new SnapshotParameters(),writableImage);
anchorPane.getChildren().remove(vBoxView);
dragboard.setDragView(writableImage);
dragboard.setContent(clipboardContent);
}
});
vBox.setOnDragOver(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent dragEvent) {
dragEvent.acceptTransferModes(TransferMode.MOVE);
Object content = dragEvent.getDragboard().getContent(dataFormat);
Person p = (Person) content;
nameField.setText(p.getName());
ageField.setText(p.getAge());
System.out.println(p.getPhoto());
imageView.setImage(new Image(p.getPhoto()));
}
});
anchorPane.getChildren().addAll(personBtn, vBox);
AnchorPane.setLeftAnchor(personBtn, 100.0);
AnchorPane.setTopAnchor(personBtn, 100.0);
AnchorPane.setLeftAnchor(vBox, 300.0);
AnchorPane.setTopAnchor(vBox, 200.0);
primaryStage.setScene(scene);
primaryStage.setTitle("Drag and Drop Image Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class Person implements Serializable {
String name;
String age;
String photo;
public Person(String name, String age, String photo) {
this.age = age;
this.name = name;
this.photo = photo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
}