This code snippet demonstrates how to change a JFrame
image icon using the setIconImage()
method.
package org.kodejava.swing;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
public class FrameIconExample extends JFrame {
public static void main(String[] args) {
FrameIconExample frame = new FrameIconExample();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// Set the window size and its title
frame.setSize(new Dimension(500, 500));
frame.setTitle("Frame Icon Example");
// Read the image that will be used as the application icon.
// Using "/" in front of the image file name will locate the
// image at the root folder of our application. If you don't
// use a "/" then the image file should be on the same folder
// with your class file.
try {
URL resource = frame.getClass().getResource("/logo.png");
BufferedImage image = ImageIO.read(resource);
frame.setIconImage(image);
} catch (IOException e) {
e.printStackTrace();
}
// Display the form
frame.setVisible(true);
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024