import java.awt.*;
import javax.swing.*;public class ImageViewer{
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
ImageFrame frame = new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});

}
} class ImageFrame extends JFrame{
public ImageFrame(){
setTitle("ImageViewr");
setSize(750, 450);
ImageIcon imgIcon = new ImageIcon(getClass().getResource("image/icon.png"));  
Image img = imgIcon.getImage();  
setIconImage(img);
}
}ImageIcon imgIcon = new ImageIcon(getClass().getResource("image/icon.png"));
改成
ImageIcon imgIcon = new ImageIcon("image/icon.png");
可以运行,不过打包成jar后图片看不到,要在jar同目录下有image/icon.png才可以看到。问题:
不知道加上getClass().getResource()为什么会出错呢?