2010-10-12 29 views
3

我有一個帶圖像文件夾的eclipse項目。我需要從位於此文件夾中的圖像創建一個ImageIcon,但無法弄清楚如何告訴它在文件夾中查找,而無需在我的系統上給出確切的位置,我不想在項目移動到不同的位置PC的。帶ImageIcon的Eclipse項目

代碼示例

ImageIcon ai = new ImageIcon("/somedir"); 

感謝您的幫助。

+0

你能告訴我們你想剛纔的代碼? – 2010-10-12 10:43:08

回答

3

您必須將圖像文件夾保存到您的projectand則:

java.net.URL imageURL = this.getClass().getClassLoader().getResource("path_to_image_folder"); 
ImageIcon aceOfDiamonds = new ImageIcon(imageURL); 

到圖像文件夾的路徑可以是相對於你的類或絕對的(以「/」開頭)。

你可以在這裏看看:How to use icons

0

這很容易(刪除反斜線):

ImageIcon ai = new ImageIcon("somedir");