如何修复代码,使其同时包含“保存”和“另存为”功能?

时间:2019-09-02 22:43:29

标签: java javafx save save-as

我在菜单栏中有两个按钮,其中既包含保存按钮,又包含另存为按钮。但是,我目前对他们两个都有相同的代码,并且通过提示用户他们要保存的位置来保存当前的代码。我希望保存按钮仅保存而不提示对话框,除非文件不存在。

我尝试摆弄代码以尝试找出解决方法,但没有弄清楚。

fileMenu.getItems().add(saveItem);
    saveItem.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            FileChooser saveFile = new FileChooser();
            saveFile.getExtensionFilters().add(new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg"));
            saveFile.setTitle("Save File");

            File file = saveFile.showSaveDialog(stage);
            if (file != null) {
                try {
                    WritableImage writableImage = new WritableImage(width, height);
                    canvas.snapshot(null, writableImage);
                    RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
                    ImageIO.write(renderedImage, "png", file);
                } catch (IOException ex) {
                    System.out.println("Error");
                }
            }
        }
    });
    fileMenu.getItems().add(saveAsItem);
    saveAsItem.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            FileChooser saveFile = new FileChooser();
            saveFile.getExtensionFilters().add(new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg"));
            saveFile.setTitle("Save File");

            File file = saveFile.showSaveDialog(stage);
            if (file != null) {
                try {
                    WritableImage writableImage = new WritableImage(width, height);
                    canvas.snapshot(null, writableImage);
                    RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
                    ImageIO.write(renderedImage, "png", file);
                } catch (IOException ex) {
                    System.out.println("Error");
                }
            }
        }
    });

代码当前为每个保存按钮执行完全相同的保存功能。我希望它仅提示您另存为按钮。

1 个答案:

答案 0 :(得分:3)

您的类中需要有一个文件实例字段,该字段最初分配为null。当您读入文件或进行第一次保存时,该字段将分配给该文件。当按下save按钮时,您将检查该字段是否为空,如果是,则显示对话框,就像显示save-as按钮一样。如果该字段为 not null,则只需使用您拥有的数据和该文件将文件写入磁盘。

例如(代码未经测试):

// a private instance field
private File myFile = null;
fileMenu.getItems().add(saveItem);
saveItem.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        if (myFile == null) {
            saveAs();
        } else {
            writeFile(myFile);
        }
    }
});

fileMenu.getItems().add(saveAsItem);
saveAsItem.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        saveAs();
    }
});
private void writeFile(File file) {
    if (file != null) {
        try {
            WritableImage writableImage = new WritableImage(width, height);
            canvas.snapshot(null, writableImage);
            RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
            ImageIO.write(renderedImage, "png", file);
        } catch (IOException ex) {
            System.out.println("Error");
        }
    }
}

private void saveAs() {
    FileChooser saveFile = new FileChooser();
    saveFile.getExtensionFilters().add(new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg"));
    saveFile.setTitle("Save File");

    File file = saveFile.showSaveDialog(stage);
    myFile = file; // !! 
    writeFile(file);
}