我使用下面的代码打开一个对话框,用于从JavaFX应用程序保存文件。
它在Windows机器上工作正常,但相同的代码不能在mac系统上运行。当我点击打开保存对话框时,它会显示一段时间然后关闭
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Game","*.xml"));
fileChooser.setTitle("Save game file");
File file = fileChooser.showSaveDialog(contextMenu.getScene().getWindow());
如果我在这里犯了任何错误,请帮助我。非常感谢...
保存对话框的完整代码
exportXml.setOnAction(new EventHandler<ActionEvent>() {
@SuppressWarnings("resource")
public void handle(ActionEvent e) {
FileChooser fileChooser = new FileChooser();
fileChooser.setInitialFileName("exported.xml");
fileChooser.setTitle("Save file");
File file = fileChooser.showSaveDialog(button.getScene().getWindow());
if (file != null) {
FileChannel sourceChannel = null;
FileChannel destChannel = null;
try {
try {
sourceChannel = new FileInputStream(new File("source.xml")).getChannel();
} catch (FileNotFoundException e3) {
e3.printStackTrace();
}
try {
destChannel = new FileOutputStream(file).getChannel();
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
try {
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
} catch (IOException e1) {
e1.printStackTrace();
}
}finally{
try {
sourceChannel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
destChannel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}