为java的窗口实例设置小图标,最好是PNG格式的
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Frame extends JFrame {
public static void main(String[] args) {
new Frame().launchFrame();
}
public void launchFrame() {
setSize(300,300);
setTitle("窗口示例");
//获取图片并设为窗口图标,注意,这里的images文件夹必须和Frame.java文件在同一个包中
java.net.URL imgURL = Frame.class.getResource("/images/yourImage.png");
ImageIcon imgIcon = new ImageIcon(imgURL);
Image img = imgIcon.getImage();
this.setIconImage(img);
//将窗口显示在屏幕正中
this.setLocationRelativeTo(null);
setVisible(true);
}
}