我正在使用 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);
}
有什么问题可能出在哪里吗?
最佳答案
你为什么不把包做成这样:
org.icon
并向该包添加图标。
设置图标使用这个:
ImageIcon img = new ImageIcon(Game.class.getResource("/org/Icon/NameOfIcon.png"));
game.setIconImage(img.getImage());
您的程序可以毫无问题地找到图标。
关于java - 使用 createAndShowGUI() 方法时如何将图标设置为 JFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13258375/