2014-07-12 28 views
0

我已經在Netbeans8中編寫了Java小程序來顯示jLabel中的圖像。在Netbeans中運行時,圖像顯示得很好。在HTML頁面中,圖像不會顯示。圖像與applet一起被放置在同一個目錄中。當在瀏覽器中運行JLabel圖像時,它不會顯示在Applet中

下面是代碼相關代碼:

Images = new String[5]; 
Images[0] = "Chrysanthemum.jpg"; 
Images[1] = "Desert.jpg"; 
Images[2] = "Hydrangeas.jpg"; 
Images[3] = "Lighthouse.jpg"; 
Images[4] = "Penguins.jpg"; 

try { 
    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("Jellyfish.jpg"))); 
    ImageIndex++; 
} catch (Exception ex) { 
    System.out.println(ex.toString()); 
} 

請幫助解決瀏覽器的這個問題。

+0

你的意思是你使用瀏覽器來運行applet嗎? – Tony

回答

0

您試圖獲取圖像作爲資源,因此它們應該打包在jar文件中。

+0

我如何在netbeans中做到這一點? – Azeem

+0

@NidaSulheri:我不使用NetBeans,但它肯定有一個教程,顯示如何將資源打包到jar文件中。 –

1

在IDE中,getResource()將返回以下兩種形式的絕對路徑(從java 1.7 API獲取)並獲得圖像。

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.

Otherwise, the absolute name is of the following form: modified_package_name/name.
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

所以在瀏覽器中,它不能獲得包的路徑正確的類路徑改變。

解決方案:正如Hovercraft所說,您可能需要Packaging and Deploying Desktop Java Applications並將其添加到包中。