我用Netbeans8写了一个用来在jLabel中显示图像的java小程序。当在Netbeans中运行时,图像显示得很好。在HTML页面中,图像将不会显示。图像与小程序放在同一目录中。
代码相关代码如下:
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());
}请在浏览器上帮助解决此问题。
发布于 2014-07-12 23:27:39
在集成开发环境中,getResource将返回以下两种形式的绝对路径(从java1.7API获取),您将获得图像。
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,并将其添加到与包相同的文件夹中。
发布于 2014-07-12 21:56:58
您试图将图像作为资源获取,因此应该将它们打包到jar文件中。
https://stackoverflow.com/questions/24713590
复制相似问题