在scenebuilder中有两个关于AnchorPane的图像视图。一个imageview1有图像 现在尝试拖放imageview2。
public class FXMLDocumentController implements Initializable {
@FXML
private ImageView imageview1;
@FXML
private ImageView imageview2;
@Override
public void initialize(URL url, ResourceBundle rb) {
imageview1.setImage(new Image("File:///C:imagedragndropex6/IMG_4093.JPG"));
}
@FXML
private void handleDragDetected(MouseEvent event) {
System.out.println("DragDetectedEntered");
// ImageView im=(ImageView)event.getSource();
Image img=imageview1.getImage();
Dragboard db= imageview1.startDragAndDrop(TransferMode.COPY);
// db.setDragView(imageview1.getImage());
ClipboardContent content=new ClipboardContent();
content.putImage(img);
System.out.println(content.hasImage());
db.setContent(content);
System.out.println(db.hasImage());
System.out.println("DragDetectedExited");
}
@FXML
private void handleDragOver(DragEvent event) {
System.out.println("DragOverEntered");
Dragboard db=event.getDragboard();
System.out.println(db.hasImage());
if(db.hasImage()){
event.acceptTransferModes(TransferMode.COPY);
System.out.println(event.getAcceptedTransferMode());
}
event.consume();
System.out.println("DragOverExited");
}
@FXML
private void handleDragDropped(DragEvent event) {
System.out.println("DragDroppedrEntered");
Dragboard dragboard=event.getDragboard();
if(dragboard.hasImage()){
Image img=dragboard.getImage();
imageview2.setFitHeight(200);
imageview2.setFitWidth(200);
imageview2.setPreserveRatio(true);
imageview2.setImage(img);
event.setDropCompleted(true);
}
else
event.setDropCompleted(false);
System.out.println("DragDroppedrEntered");
}
}
输出:
DragDetectedEntered
true
true
DragDetectedExited
DragOverEntered
false
DragOverExited
Plz让我知道这个尝试几个小时有什么问题...
答案 0 :(得分:0)
公共类FXMLDocumentController实现Initializable {
@FXML
private ImageView imageview1;
@FXML
private AnchorPane anchorpane;
@Override
public void initialize(URL url, ResourceBundle rb) {
imageview1.setImage(new Image("File:///D:/Downloads/Earthpic.jpg"));
// TODO
}
@FXML
private void handleDragDetected(MouseEvent event) {
System.out.println("DragDetectedEntered");
Dragboard db= imageview1.startDragAndDrop(TransferMode.COPY_OR_MOVE);
Image img=imageview1.getImage();
ClipboardContent content=new ClipboardContent();
content.putImage(img);
System.out.println(content.hasImage());
db.setContent(content);
System.out.println(db.hasImage());
System.out.println("DragDetectedExited");
event.consume();
}
@FXML
private void handleDragOver(DragEvent event) {
System.out.println("DragOverEntered");
System.out.println(event.getGestureSource());
System.out.println(event.getGestureTarget());
Dragboard db=event.getDragboard();
System.out.println(db.hasImage());
if(db.hasImage()){
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
System.out.println(event.getAcceptedTransferMode());
}
event.consume();
System.out.println("DragOverExited");
}
@FXML
private void handleDragDropped(DragEvent event) {
System.out.println("DragDroppedrEntered");
Dragboard dragboard=event.getDragboard();
if(dragboard.hasImage()){
Image img=dragboard.getImage();
ImageView imageview2=new ImageView();
imageview2.setFitHeight(200);
imageview2.setFitWidth(200);
imageview2.setLayoutX(400);
imageview2.setLayoutY(150);
imageview2.setPreserveRatio(true);
imageview2.setImage(img);
anchorpane.getChildren().add(imageview2);
event.setDropCompleted(true);
}
else
event.setDropCompleted(false);
System.out.println("DragDroppedrEntered");
}
}
最后,这很好用... handleDragOver和handleDragDropped必须设置为anchorpane。