2011-12-21 44 views
0

我正在設計一個帶有自定義按鈕的計算器。當然,我想組織我的圖像文件與包界面不同的文件夾。 文件夾的位置是接口/七/七normal.png但每當我不包括完整鏈接在不同的文件夾中使用圖像鏈接(Java)

"C:\Liloka\Source\interfaces\Seven\seven-normal.png" 

它不工作,一切都消失了。我發誓我已經看到這是在正常的代碼完成。如果我在正確的程序中使用它,我不能指望人們將鏈接改變到他們放置代碼的地方!這裏是我一直在使用的代碼:

seven = new ButtonImage("/Seven/seven-normal.png"); - Doesn't work 
    nine = new ButtonImage("C:/Users/Liloka/workspace/WebsiteContent/src/interfaces/Nine/nine-normal.png"); - Does work 

謝謝!

回答

2
"/Seven/seven-normal.png" 

...是C:\Seven\seven-normal.png的路徑 - 因爲/在你的路徑,這基本上意味着一開始,from the root of the drive, go to the "Seven" folder, and then load "seven-normal.png"

你必須使用相對路徑,像,"../../interfaces/Seven/seven-normal.png"或也許只是"interfaces/Seven/seven-normal.png"

第一個路徑將帶你「上」兩個文件夾,然後下到interfaces/Seven/seven-normal.png。從本質上講,你必須弄清楚你的代碼在哪個文件夾下運行,也稱爲「工作目錄」,並從那裏構建一個相對路徑。

+0

啊對..我認爲這將意味着去接口\七,就好像該文件尋找任何東西,如另一類,只要它在接口,對我來說是第一個地方,它會尋找任何東西;但顯然不是。謝謝! – liloka 2011-12-21 22:54:38

2

只需刪除第一個正斜槓即可。

seven = new ButtonImage("interfaces/Seven/seven-normal.png"); 

interfaces是應該與JAR位於同一文件夾中的文件夾。

相關問題