好的,这个问题真的很奇怪。每次我的应用程序打开JFileChooser时,它都会抛出一个IOException异常,然后一些图标显示不正确。
java.io.IOException
at sun.awt.image.GifImageDecoder.readHeader(GifImageDecoder.java:265)
at sun.awt.image.GifImageDecoder.produceImage(GifImageDecoder.java:102)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
现在,当我深入分析这个错误时,似乎只有一个图标在尝试读取头文件时仅检索到了前8个字节,这是不够的。我已经检查了所有图标文件,它们都是正常的。 我尝试用另一个在此错误之前正确加载的图标文件来覆盖该图标文件,但结果相同。
以下是我的堆栈跟踪信息:
Daemon Thread [Image Fetcher 0] (Suspended (exception IOException))
GifImageDecoder.readHeader() line: 265 [local variables unavailable]
GifImageDecoder.produceImage() line: 102 [local variables unavailable]
ByteArrayImageSource(InputStreamImageSource).doFetch() line: 246
ImageFetcher.fetchloop() line: 172
ImageFetcher.run() line: 136 [local variables unavailable]
当我深入GifImageDecoder实例时,这是我的变量值。
source ByteArrayImageSource (id=272)
awaitingFetch false
consumers null
decoder GifImageDecoder (id=271)
decoders GifImageDecoder (id=271)
imagedata (id=307)
[0] 71
[1] 73
[2] 70
[3] 56
[4] 57
[5] 97
[6] 16
[7] 13
[8] 10
imagelength 9
imageoffset 0
通常,这个图像数据应该更大。前10个字节是头文件,但是只检索了8个字节,如您所见。在此异常之后,JFileChooser中的每个其他图标都无法正确加载。
这是一个正确调用readHeader()的示例:
source ByteArrayImageSource (id=208)
awaitingFetch false
consumers null
decoder GifImageDecoder (id=207)
decoders GifImageDecoder (id=207)
imagedata (id=223)
[0...99]
[100...199]
[200...299]
[300...399]
[400...499]
[500...599]
[600...699]
[700...799]
[800...899]
[900...979]
imagelength 980
imageoffset 0
缓冲区已经完全加载了一个图标,就在抛出错误的那个图标之前。
以下是可能导致程序崩溃的示例(这种情况在我的代码中的多个部分都发生过,每当我首次加载系统图标时):
public class DirectoryBrowser extends JFileChooser{
private String suffixAccepted = null;
public DirectoryBrowser(File file, String chooserTitle, String approveOpenBtnText, String suffixAccepted)
{
super(file);
this.suffixAccepted = suffixAccepted;
init(chooserTitle, approveOpenBtnText);
}
当它进入超级(file)时,它会到那里:
Thread [AWT-EventQueue-0] (Suspended)
Object.wait(long) line: not available [native method]
MediaTracker.waitForID(int, long) line: 651
ImageIcon.loadImage(Image) line: 234
ImageIcon.<init>(byte[]) line: 215
SwingUtilities2$2.createValue(UIDefaults) line: 1105
UIDefaults.getFromHashtable(Object) line: 185
UIDefaults.get(Object) line: 130
MultiUIDefaults.get(Object) line: 44
MultiUIDefaults(UIDefaults).getIcon(Object) line: 411
UIManager.getIcon(Object) line: 613
IronFileChooserUI(BasicFileChooserUI).installIcons(JFileChooser) line: 233
IronFileChooserUI(BasicFileChooserUI).installDefaults(JFileChooser) line: 219
IronFileChooserUI(BasicFileChooserUI).installUI(JComponent) line: 135
IronFileChooserUI(MetalFileChooserUI).installUI(JComponent) line: 139
DirectoryBrowser(JComponent).setUI(ComponentUI) line: 653
DirectoryBrowser(JFileChooser).updateUI() line: 1757
DirectoryBrowser(JFileChooser).setup(FileSystemView) line: 366
DirectoryBrowser(JFileChooser).<init>(File, FileSystemView) line: 332
DirectoryBrowser(JFileChooser).<init>(File) line: 315
DirectoryBrowser.<init>(File, String, String, String) line: 33
PackToIntegratePanel.choosePackPathToIntegrateFile() line: 522
PackToIntegratePanel$1.actionPerformed(ActionEvent) line: 104
JButton(AbstractButton).fireActionPerformed(ActionEvent) line: 1849
AbstractButton$Handler.actionPerformed(ActionEvent) line: 2169
DefaultButtonModel.fireActionPerformed(ActionEvent) line: 420
DefaultButtonModel.setPressed(boolean) line: 258
BasicButtonListener.mouseReleased(MouseEvent) line: 236
JButton(Component).processMouseEvent(MouseEvent) line: 5517
JButton(JComponent).processMouseEvent(MouseEvent) line: 3135
JButton(Component).processEvent(AWTEvent) line: 5282
JButton(Container).processEvent(AWTEvent) line: 1966
JButton(Component).dispatchEventImpl(AWTEvent) line: 3984
JButton(Container).dispatchEventImpl(AWTEvent) line: 2024
JButton(Component).dispatchEvent(AWTEvent) line: 3819
LightweightDispatcher.retargetMouseEvent(Component, int, MouseEvent) line: 4212
LightweightDispatcher.processMouseEvent(MouseEvent) line: 3892
LightweightDispatcher.dispatchEvent(AWTEvent) line: 3822
WorkbenchFrame(Container).dispatchEventImpl(AWTEvent) line: 2010
WorkbenchFrame(Window).dispatchEventImpl(AWTEvent) line: 1791
WorkbenchFrame(Component).dispatchEvent(AWTEvent) line: 3819
EventQueue.dispatchEvent(AWTEvent) line: 463
EventDispatchThread.pumpOneEventForHierarchy(int, Component) line: 242
EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: 163
EventDispatchThread.pumpEvents(int, Conditional) line: 157
EventDispatchThread.pumpEvents(Conditional) line: 149
EventDispatchThread.run() line: 110
然后上面提到的另一个线程检索图标(参见本帖子的第二个堆栈)。