我在运行我的代码时遇到了一些问题,这些代码在 Windows 下运行,现在我在 Mac 上。这与拖放有关,它曾经在 Windows 上工作(一次编写,到处调试!)但现在没有触发 onDragDropped 事件(我已经使用 System.out.println() 进行了测试)。这是代码。任何帮助表示赞赏。提前致谢
HRWindowGridPane.setOnDragEntered(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
System.out.println("Drag entered");
lblPhoto.setEffect(finalBlend);
event.consume();
}
});
HRWindowGridPane.setOnDragExited(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
lblPhoto.setEffect(null);
System.out.println("Drag exited");
event.consume();
}
});
EventHandler<DragEvent> onDragOver = new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
/* data is dragged over the target */
/* accept it only if it is not dragged from the same node
* and if it has a string data */
if (event.getGestureSource() != HRWindowGridPane &&
event.getDragboard().hasFiles()) {
/* allow for both copying and moving, whatever user chooses */
event.acceptTransferModes(TransferMode.ANY);
System.out.println("Drag over");
}
event.consume();
}
};
HRWindowGridPane.setOnDragOver(onDragOver);
HRWindowGridPane.setOnDragDropped(new EventHandler<DragEvent>()
{
@Override
public void handle(DragEvent event)
{
System.out.println("Drag dropping");
Dragboard db = event.getDragboard();
System.out.println("Drag dropped: got dragboard from event!");
if(db.hasFiles())
{
System.out.println("Drag dropped: dragboard has files!");
for(File file:db.getFiles())
{
if(file.getName().contains(".jpg") || file.getName().contains(".jpeg") || file.getName().contains(".JPG"))
{
Image img;
try
{
System.out.println("Everything is fine until image processing... hmmm");
img = new Image(file.toURI().toURL().toString(), 118.0, 88.0, false, true);
ImageView view = new ImageView(img);
view.setFitWidth(118.0);
view.setFitHeight(88.0);
view.resize(118.0, 88.0);
lblPhoto.setGraphic(view);
avatarImage = file;
}
catch (IOException e)
{
e.printStackTrace();
}
}
else
{
AlertWindow alert = new AlertWindow("Falha", "O Arquivo não é uma imagem JPG válida");
try
{
Stage stage = new Stage();
alert.start(stage);
stage.toFront();
HRWindowGridPane.toBack();
stage.centerOnScreen();
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
event.setDropCompleted(true);
event.consume();
}
});
HRWindowGridPane.setOnDragDone(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event)
{
System.out.println("Drag done");
}
});
有关系统的更多信息:
Brunos-MacBook-Pro:~ bruno$ java -version
java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b21)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)
Javafx 2.1 版