Java Placing Frame Icon
Summary: Learn in this tutorial "Java Placing Frame Icon", how to place your own icon on the frame (in place of default coffee cup) title bar.
In the following program, our own choice of icon (bird) is placed on the frame title bar. It replaces the default coffee cup. The frame is also made non-resizable with setResizable() method.
import java.awt.*;
import java.awt.event.*;
public class FrameIcon extends Frame
{
public FrameIcon()
{
setTitle("Frame Icon");
Image img = Toolkit.getDefaultToolkit().getImage("bird2.gif");
setIconImage(img);
setResizable(false);
setSize(350, 200);
setVisible(true);
}
public static void main(String args[])
{
new FrameIcon();
}
}
Observe the bird image on the title bar.
Image img = Toolkit.getDefaultToolkit().getImage("bird2.gif");
setIconImage(img);
Any image to be placed on the frame should be converted into an object of java.awt.Image class. The image object is returned by getImage() method of Toolkit class.
setResizable(false);
The above statement makes the frame non-resizable by the user.
All the remaining code is as usual.
sir!why the image is working only if image is of gif format,why it so?
Generally all software do not support all formats. Images are many formats like .jpg, .gif, .bmp, .png etc.
sir!!!replacing coffee cup program is not working for me..i have exceuted the same code…can you tell the reason
See the image is placed in your current directory. Image must of .gif format.
I think you checked the following link only.
http://way2java.com/awt-components/frame-icon/
Else contact me in the class.