java 如何为 Swing 应用程序设置图标图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7194734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set icon image for swing application?
提问by IAmYourFaja
I've seen many different examples showing how to set a JFrame's IconImage
so that the application uses that icon instead of the standard coffee mug. None of them are working for me.
我看过许多不同的示例,展示了如何设置 JFrameIconImage
以便应用程序使用该图标而不是标准的咖啡杯。他们都没有对我来说有效。
Here's "my" code (heavily borrowed from other posts and the internet at large):
这是“我的”代码(大量借用其他帖子和整个互联网):
public class MyApp extends JFrame
{
public MyApp()
{
ImageIcon myAppImage = loadIcon("myimage.jpg");
if(myAppImage != null)
setIconImage(myAppImage.getImage());
}
private ImageIcon loadIcon(String strPath)
{
URL imgURL = getResource(strPath);
if(imgURL != null)
return new ImageIcon(imgURL);
else
return null;
}
}
This code fails down in loadIcon
when making a call to the getResource()
method. To me, there's only 2 possibilities here: (1) the myImage.jpg
is in the wrong directory, or (2) getResource()
doesn't like something about my image (I had to convert it from CMYK to RGB in Photoshop so I could use the same image elsewhere with ImageIO
.)
loadIcon
调用该getResource()
方法时,此代码失败。对我来说,这里只有两种可能性:(1)myImage.jpg
位于错误的目录中,或者 (2)getResource()
不喜欢我的图像的某些内容(我必须在 Photoshop 中将其从 CMYK 转换为 RGB,以便我可以使用相同的图像在别处用ImageIO
.)
I have used the System.out.println(new File(".").getAbsolutePath());
trick to locate the directory where the image JPG should be stored, and still nothing worked. I have subsequently placed the JPG in just about every directory inside my project, just to rule file location out as the culprit.
我已经使用这个System.out.println(new File(".").getAbsolutePath());
技巧来定位应该存储图像 JPG 的目录,但仍然没有任何效果。我随后将 JPG 放在我项目中的几乎每个目录中,只是为了将文件位置排除为罪魁祸首。
So that leaves me to believe there's something that getResource()
doesn't like about the JPG itself. But I have now already exhausted my understanding of images and icons in the mighty, wide world of Swing.
所以这让我相信getResource()
JPG 本身有一些不喜欢的地方。但我现在已经用尽了我对 Swing 强大而广阔的世界中的图像和图标的理解。
My JPG loads fine in other image viewers, so that's ruled out as well. Anyone have any ideas?
我的 JPG 可以在其他图像查看器中正常加载,因此也排除了这种情况。有人有想法么?
Thanks in advance!
提前致谢!
回答by Anantha Sharma
put the image in the root of the classpath and say getResource("classpath:myimage.jpg");
将图像放在类路径的根目录中并说 getResource("classpath:myimage.jpg");
The problem with your code is that jvm is unsure where to lookup the image file so its returning null.
您的代码的问题在于 jvm 不确定在哪里查找图像文件,因此它返回 null。
Here is a nice link about classpath
这是关于类路径的一个很好的链接
回答by Vidu
I have tried following which was a answer for same kind of questionlike yours. And it works for me.
Try putting your images in a separate folder outside of your src folder. Then, use ImageIO to load your images. (answered Aug 27 '13 at 0:18 AndyTechGuy)
尝试将图像放在 src 文件夹之外的单独文件夹中。然后,使用 ImageIO 加载您的图像。(2013 年 8 月 27 日 AndyTechGuy 于 0:18 回答)
frame.setIconImage(ImageIO.read(new File("res/icon.png")));
回答by oliholz
It should be
它应该是
if(imgURL != null)
^
instead of
代替
if(imgURL !- null)
and
和
URL imgURL = this.getClass().getResource(strPath);
instead of
代替
URL imgURL = getResource(strPath);
Then it works fine, if "myimage.jpg"
is in the same dir with MyApp.class
然后它工作正常,如果"myimage.jpg"
与MyApp.class
回答by Sanjay Manohar
Two suggestions:
两个建议:
- Try using the
getClass().getResource("x.jpg")
, and putting the file directly in the same folder as the.class
file of the class you are in. - Make sure the name is identical in case - some operating systems are case sensitive, and within a JAR, everything is case sensitive.
- 尝试使用
getClass().getResource("x.jpg")
, 并将文件直接放在与.class
您所在班级的文件相同的文件夹中。 - 确保名称相同以防万一——某些操作系统区分大小写,而在 JAR 中,所有内容都区分大小写。