JFrame 中未显示图像 - Java 应用程序

Image not showing in JFrame - Java Application

为了添加图片,我使用了这段代码。

JLabel label = new JLabel(new ImageIcon(getClass().getResource("/bin/image.png")));

图片位置:Application/src/bin/image.png.

问题是,在我的电脑调试模式下显示图像。但是在清理并构建项目后,如果我将文件夹 (Application/dist) 复制到另一台计算机,它不会显示图像。如何在其他电脑上显示图片?

getClass().getResource 作为文件从 /src/ 包中返回,将在构建后打包到 bin 中。

更改为仅 /image.png 以从项目结构加载它

否则,如果您想复制资源(或通过源添加新项目),则必须使用完整的限定资源路径名并将其作为文件加载。

此外,您需要通过 Eclipse 或 FatJar 导出资源,不知道通过 Ant。

我在使用 getClass().getResource 获取资源时也遇到了一些问题,但我确实使用了 getClass().getClassLoader().getResource。