我尝试了这种方式,但它没有改变?
ImageIcon icon = new ImageIcon("C:\\Documents and Settings\\Desktop\\favicon(1).ico");
frame.setIconImage(icon.getImage());
建议使用 .png 格式的文件,.ico 格式是Windows专用的。最好不要使用文件,而是使用类资源(可以打包在应用程序的jar文件中)。
URL iconURL = getClass().getResource("/some/package/favicon.png");
// iconURL is null when not found
ImageIcon icon = new ImageIcon(iconURL);
frame.setIconImage(icon.getImage());
虽然您甚至可以考虑使用 setIconImages 为图标提供多种尺寸。
/
区分大小写。 - Joop Eggenico
文件。更换为 PNG 格式后就可以了! - JFreeman试着将你的图片放在一个独立的文件夹中,不要放在src文件夹里面。然后使用ImageIO来加载你的图片。代码应该像这样:
frame.setIconImage(ImageIO.read(new File("res/icon.png")));
最终我发现设置jframe图标的主要问题。这是我的代码。它类似于其他代码,但需要注意几个细节。
this.setIconImage(new ImageIcon(getClass().getResource("Icon.png")).getImage());
1) 将这段代码放在jframe窗口打开事件中
2) 将图像放在所有表单和java文件创建的主文件夹中,例如:
src\ myproject\ myFrame.form
src\ myproject\ myFrame.java
src\ myproject\ OtherFrame.form
src\ myproject\ OtherFrame.java
src\ myproject\ Icon.png
3) 最重要的是文件名区分大小写,例如icon.png不起作用,但Icon.png可以。
这样做可以确保即使在最终构建项目后,您的图标仍然存在。
这对我起作用。
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(".\\res\\icon.png"));
URL url = Main.class.getResource("/icon.png");
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(url));
只需将这几行代码复制到您的代码中,并将“imgURL”替换为您想要设置为jframe
图标的图像位置。
JFrame.setDefaultLookAndFeelDecorated(true);
//Create the frame.
JFrame frame = new JFrame("A window");
//Set the frame icon to an image loaded from a file.
frame.setIconImage(new ImageIcon(imgURL).getImage());
以下是我使用的代码,用于设置 JFrame
的图标。
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
try{
setIconImage(ImageIO.read(new File("res/images/icons/appIcon_Black.png")));
}
catch (IOException e){
e.printStackTrace();
}
myFrame.setIconImage(Toolkit.getDefaultToolkit().getImage("Icon.png"));
我使用 Maven
并且拥有项目结构,该结构是通过输入命令创建的:
mvn archetype:generate
必须将所需文件 icon.png
放置在您的Maven项目的 src/main/resources
文件夹中。
然后您可以在项目内使用以下代码:
ImageIcon img = new ImageIcon(getClass().getClassLoader().getResource("./icon.png"));
setIconImage(img.getImage());
ImageIcon img = new ImageIcon(Objects.requireNonNull(getClass().getClassLoader()
.getResource("konashop/com/icon.png"))); // the URL should be within the src with the classes
frame.setIconImage(img.getImage());
我正在使用以下实用类为 JFrame
和 JDialog
实例设置图标:
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Scanner;
public class WindowUtilities
{
public static void setIconImage(Window window)
{
window.setIconImage(Toolkit.getDefaultToolkit().getImage(WindowUtilities.class.getResource("/Icon.jpg")));
}
public static String resourceToString(String filePath) throws IOException, URISyntaxException
{
InputStream inputStream = WindowUtilities.class.getClassLoader().getResourceAsStream(filePath);
return toString(inputStream);
}
// https://dev59.com/UHVC5IYBdhLWcg3wZwJT#5445161
private static String toString(InputStream inputStream)
{
try (Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\A"))
{
return scanner.hasNext() ? scanner.next() : "";
}
}
}
因此,使用它就像调用一样简单
WindowUtilities.setIconImage(this);
在扩展JFrame
的类中的某个位置。
例如使用Maven
时,Icon.jpg
必须位于myproject\src\main\resources
中。