使用JFileChooser保存BufferedImage会引发一些问题

use*_*233 1 java swing bufferedimage jfilechooser nimbus

我有一个相当简单的绘图程序,它使用BufferedImage作为画布.我有一个单独的类(IOClass)来处理保存图像和打开另一个图像.我通过我的saveImage()方法保存BufferedImage有点麻烦.这是整个班级:

 package ui;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

// class for Saving & Opening images (for the bufferedImage called 'background'
public class IOClass {
    static BufferedImage image;

    public IOClass(BufferedImage image) {
        this.image = image;
    }

    public static final JFileChooser fileChooser = new JFileChooser();
    public void saveImage() {
        int saveValue = fileChooser.showSaveDialog(null);
        if (saveValue == JFileChooser.APPROVE_OPTION) {
            try {
                ImageIO.write(image, "png", new File(fileChooser
                        .getSelectedFile().getAbsolutePath()
                        + fileChooser.getSelectedFile().getName()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public BufferedImage openImage() {
        int open = fileChooser.showOpenDialog(null);

    }
}
Run Code Online (Sandbox Code Playgroud)

因此,正如您通过阅读saveImage()方法所看到的,这就是问题所在.程序打开,你绘制一张图片,你带着'另存为'菜单选项转到JMenuBar,它激活一个actionListener,打开这个类并启动新的fileChooser,你可以使用JFileChooser保存图像.图像拒绝保存,而是引发IllegalArguementException.问题必须出在这个Save方法中,我假设它发生在ImageIO.write(bla bla bla)方法中.我该怎么做才能确保此图像正确保存,以及我究竟做错了什么?我已经阅读了一些JFileChooser API,我认为这是其中唯一真正重要的部分,但请告诉我是否应该回去添加一些内容.谢谢.

额外:仅当用户按下主程序(未显示)的JMenuBar上的"另存为"按钮时,JFileChooser才会出现.主程序使用"Nimbus"主题,可以使用代码段来使用:

try {
            UIManager
                    .setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

当我稍早尝试某些东西时,打开JFileChooser也打开了Nimbus主题,但现在,它只打开了正常,无聊,默认的Swing外观.我能做些什么来恢复Nimbus主题(它看起来好多了).

编辑:根据要求,完整堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: image == null!
    at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(Unknown Source)
    at javax.imageio.ImageIO.getWriter(Unknown Source)
    at javax.imageio.ImageIO.write(Unknown Source)
    at ui.IOClass.saveImage(IOClass.java:26)
    at ui.ProgramUI$saveAsListener.actionPerformed(ProgramUI.java:406)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Run Code Online (Sandbox Code Playgroud)

Hov*_*els 5

线程"AWT-EventQueue-0"中的异常java.lang.IllegalArgumentException:image == null!

该异常表明它被抛出是因为你的图像是null,所以问题不在上面的代码中,而是在调用上面代码的代码中:你传入一个空图像.

为了证明这一点,测试它:

    if (saveValue == JFileChooser.APPROVE_OPTION) {
      System.out.println("is image null? " + (image == null));
      try {
        // ....
Run Code Online (Sandbox Code Playgroud)

这些人一次又一次:

static BufferedImage image;
public static final JFileChooser fileChooser = new JFileChooser();
Run Code Online (Sandbox Code Playgroud)

不应该是静态的.同样,JFileChooser可能会因此失去Nimbus L&F,因为它是在类加载之前启动的,在您设置L&F之前启动,而不是在实例化IOClass的对象时启动.


编辑
作为一个旁边,不应该这样:

ImageIO.write(image, "png", new File(fileChooser
                    .getSelectedFile().getAbsolutePath()
                    + fileChooser.getSelectedFile().getName()));
Run Code Online (Sandbox Code Playgroud)

是吗?:

ImageIO.write(image, "png", fileChooser.getSelectedFile());
Run Code Online (Sandbox Code Playgroud)