我嘗試使用JLabel
顯示圖像。這是我的項目導航: 將圖像加載到JLabel中不起作用
從SettingsDialog.java
我想用顯示圖像下面的代碼:
String path = "/images/sidebar-icon-48.png";
File file = new File(path);
Image image;
try {
image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
header.add(label); // header is a JPanel
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
代碼拋出一個異常:無法讀取輸入文件!
圖像的路徑是否錯誤?
該文件的路徑不正確。例外情況非常明顯。 –
請看看[這個答案](http://stackoverflow.com/a/9866659/1057230)對於__Eclipse__相關的部分,可能會對此有所贅述。此鏈接也可在[tag:embedded-resource] –