【问题标题】:Java applet not able to find resource image with ImageIcon unless entire path is used除非使用整个路径,否则 Java 小程序无法使用 ImageIcon 找到资源图像
【发布时间】:2014-07-12 03:02:22
【问题描述】:

我已将图像 TestIcon.png 放在 Java 小程序的 src 文件夹中(目录为 C:\Users\User\workspace\applettest2\src\TestIcon.png)。我读过的所有内容都表明我应该能够通过引用“TestIcon.png”来使用该图像。示例:

ImageIcon xIcon = new ImageIcon("TestIcon.png");

但是,在 Eclipse 中使用此代码运行小程序不起作用;图像不显示。如果我输入图像的整个路径:

ImageIcon xIcon = new ImageIcon("C:\\Users\\User\\workspace\\applettest2\\src\\TestIcon.png");

然后小程序会正确显示图像。资源的默认路径不应该在“src”文件夹中吗?谢谢。

【问题讨论】:

    标签: java eclipse image applet


    【解决方案1】:

    如果您在 Applet 中运行它,请尝试使用 Applet#getCodeBase()Applet#getDocumentBase

    Image image = getImage(getDocumentBase(), "TestIcon.png");
    

    查找更多示例 HereHere

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      • 2016-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多