0

I've written a game that uses PNGs, and when I exported it, the images wouldn't work. I added getClass().getClassLoader().getResource() to everywhere I import an image, but the JAR won't even launch anymore. It used to open, but none of the images worked. Now it won't even open the JAR.

This is how I get the image for the muffin:
muffin=new ImageIcon(getClass().getClassLoader().getResource("muffin.png")).getImage(); Actual path for it is: C:\Users\My User Name\Dropbox\FinalProjectWithoutApplet\muffin.png

What should I do to solve this issue?

Thanks!

4

2 回答 2

0

getClass().getClassLoader().getResource("muffin.png")muffin.png 正在与您的班级所在的同一目录中查找图像。

我的意思是,如果您的班级名称是图像,则com.mycompany.game.MyClass该图像应位于com/mycompany/game/muffin.png. 如果这不是位置,请写入以 开头的绝对路径,/例如/img/muffin.png

并且将来如果程序不工作从检查堆栈跟踪开始。

于 2013-05-26T20:07:10.183 回答
0

我发现了这个问题。原来我的情况不匹配。IDE 并不关心"muffin.png"在代码与"Muffin.png". 我发现的另一个案例是"DoubleJumpIcon.png"vs. "DoubleJumpIcon.PNG". 事实证明,每一个案例都很重要。

于 2013-05-27T16:02:42.967 回答