Following program shows how to change a JFrame image icon. In this program I have use the setIconImage() method to do this. In this program I have use javax.imageio.ImageIO class to input image into our program. I also have use javax.swing.JFrame class to install frame into our code. To store image temporarily I have use java.awt.image.BufferedImage class.
Code:
package sample.code.java.example.swing;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import java.awt.Dimension;
import java.io.IOException;
public class FrameIconEg extends JFrame {
public static void main(String[] args) {
FrameIconExample frames = new FrameIconExample();
frames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frames.setSize(new Dimension(350, 250));
frames.setTitle("Example of Frame Icon");
BufferedImage images = null;
try {
images = ImageIO.read(frames.getClass().getResource("/colors.jpg"));
} catch (IOException es) {
es.printStackTrace();
}
frames.setIconImage(images);
frames.setVisible(true);
}
}
Bookmarks