How to set an icon for a frame in Java?

拟墨画扇 提交于 2019-12-08 12:59:33

问题


I have used this statement to set icon of a frame:

frame.setIconImage(Toolkit.getDefaultToolkit().getImage("tictactoe.gif"));

Unfortunately the frame icon still shows default java icon instead.

How to fix this?


回答1:


Check if Toolkit.getDefaultToolkit().getImage("tictactoe.gif") really reads the image. Try to split into 2 lines:

Image img = Toolkit.getDefaultToolkit().getImage("tictactoe.gif");
frame.setIconImage (img);

Then use debugger to check what is there inside img variable.




回答2:


Try with ImageIcon and let us now if it works:-). It has a simple constructor taking a path to file with icon image. After construction you can retrieve image from it by calling ImageIcon.getImage().

Maybe the image is not in the right place and cannot be found?



来源:https://stackoverflow.com/questions/2370526/how-to-set-an-icon-for-a-frame-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!