2014-07-12 29 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并将其添加到包中。