使用createAndShowGUI()方法时如何将图标设置为JFrame? [英] How to set an icon to a JFrame when using createAndShowGUI() method?

查看:831
本文介绍了使用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"));

显示图像所在的文件夹的方法& amp ;如有必要,请将其复制。

to the method to display the folder where the image should be located & copy it there if necessary.

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

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