[英]JLabel image is not displayed in Applet when run it in browser
我已经在Netbeans8中编写了Java applet以在jLabel
显示图像。 在Netbeans中运行时,图像显示良好。 在HTML页面中,图像不会显示。 图像与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());
}
请帮助解决浏览器上的此问题。
在IDE中, getResource ()将返回以下两种形式的绝对路径(从Java 1.7 API获取),您将获得映像。
If the name begins with a '/' ('\/'), 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 '.' ('\.').
因此,在浏览器中,它无法获得更改包路径的正确类路径。
解决方案:正如Hovercraft所说,您可能需要打包和部署桌面Java应用程序 ,并将其添加到与程序包相同的文件夹中。
您正在尝试将图像作为资源,因此应将其打包在jar文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.