简体   繁体   English

设置图标图像JFrame

[英]Set Icon Image JFrame

I have a .ico file I want to use as the title icon for my program. 我有一个.ico文件,想要用作程序的标题图标。 I'm pretty sure I have to use the setIconImage(image) method but every way I've tried doing this has ended in failure. 我很确定我必须使用setIconImage(image)方法,但是我尝试执行的所有方法都以失败告终。

I've tried quite a few different methods but none have worked for me. 我尝试了很多不同的方法,但没有一个对我有用。 One I thought would have worked was the following: 我以为会工作的是以下内容:

    Image image = new ImageIcon(this.getClass().getResource("Icon.ico").getImage());
    setIconImage(image);

Only problem with this however is the type mismatch. 但是,唯一的问题是类型不匹配。 Both the main class and the icon are in the same package but I can't seem to set it no matter how hard I try. 主类和图标都在同一个包中,但是无论我怎么努力,我似乎​​都无法设置它。

What would be the correct method for doing this? 正确的方法是什么?

The best way is probably to use ImageIO to read the Image from the jar. 最好的方法可能是使用ImageIO从jar中读取图像。

Image image = ImageIO.read(this.getClass().getResource("/me/spedwards/program/Icon.ico"));
setIconImage(image);

This works for me 这对我有用

setIconImage(new ImageIcon("Icon.ico").getImage());

Here are a few options with a demo 3 ways to Set Icon for JFrame 这是演示的一些选项,演示为JFrame设置图标的3种方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM