如何更改程序图标
我尝试查找多个有关如何更改 java 中的程序图标的教程,但似乎都不起作用。我还想知道它可以是 .ico 或 .png 什么样的图像,以及它的大小必须是多少
I tried looking up multiple tutorials on how to change the programs icon in java but none of them seem to work. I was also wondering what kind of image can it be .ico or .png eg and what size it had to be
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您使用的是
JFrame
,那么您需要setIconImage(java.awt.Image)
:只要 ImageIO 可以读取图像文件格式即可(JPEG 、GIF、PNG、TIFF,甚至 BMP(如果确实有必要的话)。
示例:
编辑:如何获取图标的 URL
您应该将图像文件(图标等)与源代码一起放置在
src< 中其自己的子文件夹中/code> 文件夹并将它们作为“资源”访问。查看 Java 教程中的如何使用图标,了解如何获取资源的
URL
对象。I assume you're using a
JFrame
, then you wantsetIconImage(java.awt.Image)
:The image file format is irrelevant as long as ImageIO can read it (JPEG, GIF, PNG, TIFF, even BMP if really really necessary).
An example:
Edit: How to get a URL to your icon
You should place your image files (icons, etc) along with your source code, maybe into its own sub folder in your
src
folder and access them as "resource". Check out How to use Icons from the Java Tutorial for how to get aURL
object to a resource.我认为@Philipp说得很清楚,
x
必须是File
、InputStream
或URL
,而不是一个String
就像你尝试过的那样。现在,您还需要有一个
Frame
或JFrame
才能有图标!我认为这是在您的
main
方法中。要创建一个框架,请尝试
使用 @Philipp 的代码。
--
我突然想到你可能指的是操作系统中程序快捷方式的图标。这不是在 Java 中完成的,而是在您的操作系统中完成的。除非你使用Java Web start
I think @Philipp made it quite clear, that
x
has to be aFile
,InputStream
orURL
, not aString
as it seems you tried.Now, you also need to have a
Frame
orJFrame
to have an icon!I presume this is in your
main
method.To create a frame try
followed by @Philipp's code.
--
It just occured to me you might mean the icon of the program shortcut in your operating system. This isn't done in Java but in your operating system. unless you use Java Web start