本文介绍了使用 createAndShowGUI() 方法时如何将图标设置为 JFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 createAndShowGUI()
方法来创建 JFrame
.我正在尝试设置一个图标,但是当我在 NetBeans 中运行它时,它没有显示.但是,当我运行 .jar
文件(图像在同一文件夹中)时,它可以正常工作.
I am using a createAndShowGUI()
method to create a JFrame
. I am trying to set an icon, but when I run it in NetBeans, it doesn't show. However, when I run a .jar
file (with the image in the same folder), then it works without a flaw.
private static void createAndShowGUI() {
//Create and set up the window.
JFrame game = new JFrame();
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setSize(198, 409);
game.setResizable(false);
game.setTitle("Frame Title");
ImageIcon img = new ImageIcon("Icon.png");
game.setIconImage(img.getImage());
game.setVisible(true);
}
有什么想法可能出问题吗?
Any ideas where the problem could be?
推荐答案
当我使用测试图像运行上述代码时,图标已正确更改.添加:
When I run the above code with a test image, the icon is changed correctly. Add:
System.out.println(System.getProperty("user.dir"));
显示图像应该所在文件夹的方法&如有必要,将其复制到那里.
to the method to display the folder where the image should be located & copy it there if necessary.
这篇关于使用 createAndShowGUI() 方法时如何将图标设置为 JFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!