我想用Java制作一个非常基本的游戏,但我在JFrame
上显示图像时遇到了问题。它在过去为我工作,现在不是,我看不出我做错了什么。Java将ImageIcon添加到JLabel
我已经尝试打印当前的工作目录,并更改我的图像匹配的位置。这很可能是因为我的(档案寻找器或文件读取器等)没有问题,但我无法正确地将它(ImageIcon
)添加到JLabel
或JFrame
,从而无法获取图像。
这是我的代码...
JFrame frame = new JFrame("no image");
ImageIcon image = new ImageIcon("C:/Documents and Settings/user/Desktop/hi/xD/JavaApplication2/image.png");
JLabel imagelabel = new JLabel(image);
frame.add(imagelabel);
的JFrame
已经setVisible(true)
和pack()
。
有人可以帮助我了解什么是错的。
请看看这个例子中,[如何增加图象到你的项目](http://stackoverflow.com/questions/9864267/load-icon-image-exception/9866659#9866659)或请按照这些[步骤](http:// ga gandeepbali.uk.to/gaganisonline/webpages/makejareclipse.html) –
我看了那些例子,他们没有帮助 – user1486826
只需将你的图像放在你的.class文件旁边,并像这样使用ImageIcon image = new ImageIcon(getClass( ).getResource( “yourImage.extension”));.该链接必须有效,因为这是将图片放入项目中的正确方法。我希望你已经完成了所有提到的步骤! –