gpt4 book ai didi

java - 为什么 Java ImageIcon 不是 enter catch 子句?

转载 作者:行者123 更新时间:2023-11-30 08:48:11 24 4
gpt4 key购买 nike

当我尝试创建 ImageIcon 时,出现此错误:

sun.awt.image.ImageFormatException: Unsupported color conversion request
at sun.awt.image.JPEGImageDecoder.readImage(Native Method)
at sun.awt.image.JPEGImageDecoder.produceImage(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)

如果发生此错误,那么我想加载另一个图像,所以我使用了 try-catch 方式:

  public Component getListCellRendererComponent(
JList<?> list, Object value, int index,
boolean isSelected, boolean cellHasFocus )
{
// Display the text for this item
setText(value.toString());

// Pre-load the graphics images to save time
String iconurl=ABC.getCiconUrl();
if(iconurl.isEmpty())
{
iconurl="img\\backgroundpic.png";
}
try {
image = new ImageIcon(iconurl);
} catch (Exception e) {
image = new ImageIcon("img\\backgroundpic.png");
}
// Set the correct image
setIcon( image );
return this;
}

但即使发生错误,也不会跳入catch状态。为什么 ?

最佳答案

如果您查看堆栈跟踪,您会注意到其中没有列出您的任何函数。抛出异常的线程与运行代码的线程不同;它是一个负责为 ImageIcons 异步加载图像的线程,因此您无法捕获该异常

关于java - 为什么 Java ImageIcon 不是 enter catch 子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32089018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com