如何将图标设置为 JFrame [英] How to set Icon to JFrame

查看:36
本文介绍了如何将图标设置为 JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这种方法,但没有改变?

I tried this way, but it didnt changed?

ImageIcon icon = new ImageIcon("C:\Documents and Settings\Desktop\favicon(1).ico");
frame.setIconImage(icon.getImage());

推荐答案

最好使用 .png 文件;.ico 是特定于 Windows 的.并且最好不要使用文件,而是使用类资源(可以打包在应用程序的 jar 中).

Better use a .png file; .ico is Windows specific. And better to not use a file, but a class resource (can be packed in the jar of the application).

URL iconURL = getClass().getResource("/some/package/favicon.png");
// iconURL is null when not found
ImageIcon icon = new ImageIcon(iconURL);
frame.setIconImage(icon.getImage());

尽管您甚至可能会考虑将 setIconImages 用于多种尺寸的图标.

Though you might even think of using setIconImages for the icon in several sizes.

这篇关于如何将图标设置为 JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆