简体   繁体   English

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

[英]Image not showing in JFrame - Java Application

For add an image, I used this code. 为了添加图像,我使用了这段代码。

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

image location: Application/src/bin/image.png. 图像位置: Application/src/bin/image.png.

Problem is, In my computer debugging mode it shows the image. 问题是,在我的计算机调试模式下,它显示了图像。 But after clean and built project if I copy the folder ( Application/dist ) to another computer it does not show the image. 但是,在清理并构建项目之后,如果我将文件夹( Application/dist )复制到另一台计算机,它将不会显示图像。 How can I show the image in other computers? 如何在其他计算机上显示图像?

The getClass().getResource returnas a file from within the /src/ package and will be bundled in the bin after build. getClass()。getResource从/ src /包中以文件形式返回,并将在构建后捆绑在bin中。

Changes to only /image.png to load it from the project structure 更改为/image.png以从项目结构中加载

Else if you want to copy the resources (or add new items trough source) you must use the full qualified resource path name and load it as a File instead. 否则,如果要复制资源(或通过源添加新项目),则必须使用完整的合格资源路径名并将其作为文件加载。

Also, you need to export with resources trough Eclipse or FatJar, dont know trough Ant. 另外,您还需要通过Eclipse或FatJar导出资源,而不是通过Ant导出资源。

I did also had some issues by getting resources with getClass().getResource, instead I did use getClass().getClassLoader().getResource. 通过使用getClass()。getResource获取资源,我也确实遇到了一些问题,相反,我确实使用了getClass()。getClassLoader()。getResource。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM