Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Java ImageIcon不是';不输入catch子句吗?_Java_Image_Try Catch_Imageicon - Fatal编程技术网

为什么Java ImageIcon不是';不输入catch子句吗?

为什么Java ImageIcon不是';不输入catch子句吗?,java,image,try-catch,imageicon,Java,Image,Try Catch,Imageicon,当我尝试创建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.InputStreamIm

当我尝试创建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;
}
  try {
  // The image file is in the java source project \workspace\myproject
    img = ImageIO.read(new File("image.png"));
  } catch (IOException e) {
     e.printStackTrace();
  }
公共组件getListCellRenderComponent(
JList列表、对象值、整数索引、,
布尔值(已选择,布尔单元格已聚焦)
{
//显示此项目的文本
setText(value.toString());
//预加载图形图像以节省时间
字符串iconurl=ABC.getCiconUrl();
if(iconurl.isEmpty())
{
iconurl=“img\\backgroundpic.png”;
}
试一试{
图像=新图像图标(iconurl);
}捕获(例外e){
image=新的图像图标(“img\\backgroundpic.png”);
}
//设置正确的图像
设置图标(图像);
归还这个;
}

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

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

如果加载ImageIO图像,则必须捕获如下IO异常:

  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;
}
  try {
  // The image file is in the java source project \workspace\myproject
    img = ImageIO.read(new File("image.png"));
  } catch (IOException e) {
     e.printStackTrace();
  }
使用ImageIcon,您可以使用异常对象捕获异常。然后可以使用
getResource(path)
加载图像。我这样试过,效果很好:

try {
    ImageIcon img = new ImageIcon(getClass().getClassLoader().getResource("image.png"));
  } catch (Exception e) {
    e.printStackTrace();
  }

也许这会有帮助:你确定是第一个
ImageIcon
导致问题吗?@MadProgrammer是的,我也在调试,当第一个ImageIcon打印错误到控制台时,我猜异常来自“catch block”-其中有“new ImageIcon”。。请显示完整的代码和完整的堆栈跟踪。@Jayan我添加了codesAny建议?尝试执行与ImageIcon相同的Java代码,但不使用ImageIcon(并在某些GUI组件中呈现图像)?