【发布时间】:2013-06-09 23:42:46
【问题描述】:
我有一个相当简单的绘图程序,它使用 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);
}
}
所以,正如您通过阅读saveImage() 方法所看到的,这就是问题所在。程序打开,您绘制一张图片,您使用“另存为”菜单选项转到 JMenuBar,这会激活一个打开此类的 actionListener 并启动新的fileChooser,您可以在其中使用 JFileChooser 保存图像。图像拒绝保存,而是引发 IllegalArguementException。问题必须出在这个 Save 方法中,我假设它发生在 ImageIO.write(bla bla bla) 方法中。我该怎么做才能确保正确保存此图像,以及我到底做错了什么?我已经阅读了一点 JFileChooser API,我认为这是其中唯一真正重要的部分,但如果我应该回去添加一些东西,请告诉我。谢谢。
额外: JFileChooser 仅在用户按下主程序(未显示)的 JMenuBar 上的“另存为”按钮时出现。主程序使用“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();
}
当我之前尝试某些东西时,打开 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)
【问题讨论】:
-
您的 JFileChooser 和 BufferedImage 变量不应是静态变量,而是实例变量。我猜测 JFileChooser 会因此而丢失 L&F,因为它是在类加载时启动的,而不是在实例化 IOClass 的对象时启动的。另外,也许我很困惑,但我看到您说您在使用此代码时遇到问题,但我没有看到您在哪里说明您的问题实际上是什么。
-
@HovercraftFullOfEels 我有时会担心自己......我在问题中添加了“问题”。问题是图像永远不会保存,而是我得到一个
IllegalArguementException -
请显示完整的异常文本。
-
@HovercraftFullOfEels 按要求...
标签: java swing bufferedimage jfilechooser nimbus