You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NetBeans Java项目中PNG资源加载失败问题求助

NetBeans Java项目GIMP制作PNG资源加载失败问题

在NetBeans Java项目中,从Source Packages/resources目录加载PNG图片时出现异常:部分旧PNG可正常加载,但GIMP制作的新PNG通过getClass().getResource(fileName)调用始终返回null。所有图片均为16x16尺寸,已尝试调整GIMP的多种PNG保存格式,问题仍未解决。

加载图片的代码如下:

protected ImageIcon loadIcon(String fileName) {
    Class<?> cls = this.getClass();
    URL url = cls.getResource(fileName);
    // ⬅️ 此处失败,url为null

    if (url == null) {
        return null;
    }

    Image image = null;
    try {
        image = ImageIO.read(url);
    } catch (IOException ex) {
        msglistener.Send(new EMsgData.Builder().setType(EMsgData.MsgType.DEBUG).setSender(this).setText("Load Icon Failed").build());
    }

    if (image == null) {
        return null;
    }

    return new ImageIcon(image);
}

调用示例:

ImageIcon icon = loadIcon("/Pencil.png");

附两张对比图片:
加载失败的PNG
可正常加载的PNG

内容的提问来源于stack exchange,提问作者Tim Frost

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 00:32:07