有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java文件选择器。showSaveDialog()在mac上不工作

我使用下面的代码打开一个对话框,从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();
                    }
               }

            }
        }
    }  

共 (0) 个答案