【问题标题】:Unable to find icon image using loadIcon() and getProperty()无法使用 loadIcon() 和 getProperty() 找到图标图像
【发布时间】:2013-06-18 02:58:07
【问题描述】:

我有以下语句,它给了我一个空指针异常:

Icon settingIcon =
   GUIUtilities.loadIcon(
      context.getProperty( "sys.setting.icon.small" ));

我正在尝试调试它,但我想知道它在尝试查找属性时的目录结构是什么样的。是否有可能在错误发生时找出编译器正在搜索的位置?

现在,我正在回退使用(default.png 位于根文件夹:/Users/bigbitecode/Documents/workspace/sys client_a/):

Icon saveIcon = new ImageIcon("default.png");

一些有用的信息:

  • 我已使用以下信息成功加载该属性: 在文件路径:/Users/bigbitecode/Documents/workspace/sys client_a/sys-client/gui.prop
#sys 菜单栏图标 copa.setting.icon.small=icon/16x16/setting.png
  • 我要获取的文件位于: /Users/bigbitecode/Documents/workspace/sys client_a/sys-client/resources/icon/16x16/settings.png

  • 我拥有的当前代码的路径文件位于: /Users/bigbitecode/Documents/workspace/sys client_a/sys-client/src/sysClient/data/gui/sysClient.java

【问题讨论】:

  • 希望您没有错过答案中提供的链接中的this link

标签: java swing nullpointerexception imageicon


【解决方案1】:

到部署时,这些资源可能会变成。在这种情况下,资源必须由URL 而不是File 访问。查看标签的info page,了解形成URL的方法。

getResource() 所需的可能路径是/icon/16x16/settings.png

【讨论】:

  • 我一定是做错了什么。我尝试使用 getResource 引用资源,但我似乎总是使用以下代码访问 null: URL url = this.getClass().getResource("/resources/icon/16x16/settings.png");图标 openFileIcon = new ImageIcon(url);
  • 发送jar -tvf the.jar 以确认它在您期望的位置。
  • 我做了一个 grep,并注意到 settings.png 不包括在内。这是什么意思?我在资源文件夹中都有它,但它没有显示在最终的 .jar 中。有没有办法包含所有图像?
  • “我在资源文件夹中都有,” 您似乎认为我对您的 IDE 和构建环境了解很多,但实际上我并不了解。提示:在这种情况下,更多的细节总比更少的好。 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 1970-01-01
  • 2022-11-08
相关资源
最近更新 更多