使用createAndShowGUI()方法时如何将图标设置为JFrame?
我正在使用createAndShowGUI()
方法来创建JFrame
。 我正在尝试设置一个图标,但是当我在NetBeans中运行它时,它没有显示。 但是,当我运行.jar
文件(图像在同一文件夹中)时,它没有任何缺陷。
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); }
问题可能是什么想法?
当我用测试图像运行上面的代码时,图标会正确更改。 加:
System.out.println(System.getProperty("user.dir"));
到显示图像所在文件夹的方法,必要时将其复制到那里。
你为什么不做包像:
org.icon
并为该包添加图标。
要设置图标,请使用此:
ImageIcon img = new ImageIcon(Game.class.getResource("/org/Icon/NameOfIcon.png")); game.setIconImage(img.getImage());
您的程序找到一个图标没有问题。