简体   繁体   English

Java-如何向资源添加图像

[英]Java - How to add an image to resource

I really have a problem adding images to my java project, and before you ask, yes i have searched already and tried everything, but i just can't get it working. 我确实在将图像添加到我的Java项目中时遇到问题,在您问之前,是的,我已经搜索并尝试了所有操作,但是我无法使其正常工作。

Here is my problem: 这是我的问题:

At the moment i am using this code to get the images: 目前,我正在使用此代码来获取图像:

        ImageIcon goldIcon = new ImageIcon("res/Gold_coin.png");
        ImageIcon silverIcon = new ImageIcon("res/Silver_icon.png");
        ImageIcon copperIcon = new ImageIcon("res/Copper_icon.png");

My project structure is as following: 我的项目结构如下:

I have one project folder with two sub folders. 我有一个项目文件夹,其中有两个子文件夹。

Both sub folders are specified as source folders, one is the "src" folder and the other one i named "res". 这两个子文件夹都指定为源文件夹,一个是“ src”文件夹,另一个是“ res”文件夹。 In the "src" folder i have one package with all classes in it. 在“ src”文件夹中,我有一个包含所有类的软件包。 In the "res" folder i have all the images saved. 在“ res”文件夹中,我保存了所有图像。

Now the strange thing is, the "Gold_icon" DOES work, but both silver and copper do NOT. 现在奇怪的是,“ Gold_icon”可以工作,但是银和铜都不能工作。 I am using eclipse luna and if someone could give me a step by step instruction how to add an image would be really nice. 我正在使用Eclipse Luna,如果有人可以给我逐步说明如何添加图像,那将是非常不错的。

Because all i find is always "add to resource" , "add it to resources folder" and honestly, i tried creatig a new folder, i copied it to the "src" folder, i tried every possible call, from ("res/Gold_coin.png") over ("/Gold_coin.png") to ("Gold_coin.png") and ("/res/Gold_coin.png") 因为我发现的都是“添加到资源”,“添加到资源文件夹”,说实话,所以我尝试创建一个新文件夹,将其复制到“ src”文件夹,并尝试了所有可能的调用,从(“ res / Gold_coin.png“)移至(” /Gold_coin.png“)和(” /res/Gold_coin.png“)

I refreshed the project, the folders, the package, the classes, i restarted eclipse but nothing helps 我刷新了项目,文件夹,包,类,重新启动了eclipse,但没有任何帮助

I just don't get it.. 我就是不明白。

Please help :( 请帮忙 :(

If you need the information what i want to do with this images afterwards, i am adding them together into a JPanel with flowlayout which i write into a JTable cell with a cellrenderer, which is everything working with the gold icon, but not the other two. 如果您以后需要这些图像的信息,我会将它们添加到带有flowlayout的JPanel中,并使用cellrenderer将其写入JTable单元中,这一切都使用金色图标,而其他两个都没有。 And it also does not work to remove the gold icon (because i thought maybe for whatever reason only the first icon works..) but then nothing is displayed 而且它也无法删除黄金图标(因为我以为出于任何原因也许只有第一个图标有效..),但是什么也没显示

文件夹结构和添加图像图标

向JPanel添加图像图标

输出量

You should use getResource to load images or whatever from resource folder. 您应该使用getResource加载图像或资源文件夹中的任何内容。

For example: 例如:

String pathToImage = "res/Gold_coin.png";
ImageIcon myIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImage));

or with all project path: nameOfProject/res/Gold_coin.png . 或所有项目路径: nameOfProject / res / Gold_coin.png

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

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