0

我在 Netbeans8 中编写了 java 小程序来显示jLabel. 在 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());
}

请帮助在浏览器上解决此问题。

4

2 回答 2

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 所说,您可能需要打包和部署桌面 Java 应用程序并将其添加到与包相同的文件夹中。

于 2014-07-12T15:27:38.847 回答
0

您正在尝试将图像作为资源获取,因此应将它们打包在 jar 文件中。

于 2014-07-12T13:56:57.550 回答