java调用浏览文件夹的jar包,如何访问JAR文件中的资源?

我有一个带有工具栏的Java项目,该工具栏上带有图标。这些图标存储在名为resources /的文件夹中,因此路径可能是“ resources / icon1.png”。该文件夹位于我的src目录中,因此在编译后,该文件夹将被复制到bin /

我正在使用以下代码访问资源。

protected AbstractButton makeToolbarButton(String imageName, String actionCommand, String toolTipText,

String altText, boolean toggleButton) {

String imgLocation = imageName;

InputStream imageStream = getClass().getResourceAsStream(imgLocation);

AbstractButton button;

if (toggleButton)

button = new JToggleButton();

else

button = new JButton();

button.setActionCommand(actionCommand);

button.setToolTipText(toolTipText);

button.addActionListener(listenerClass);

if (imageStream != null) { // image found

try {

byte abyte0[] = new byte[imageStream.available()];

imageStream.read(abyte0);

(button).setIcon(new ImageIcon(Toolkit.getDefaultToolkit().createImage(abyte0)));

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

imageStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

} else { // no image found

(button).setText(altText);

System.err.println("Resource not found: " + imgLocation);

}

return button;

}

(imageName将为“ resources / icon1.png”等)。在Eclipse中运行时,效果很好。但是,当我从Eclipse导出可运行的JAR时,找不到图标。

我打开了JAR文件,资源文件夹在那里。我已经尝试了所有操作,移动文件夹,更改JAR文件等,但无法显示图标。

有人知道我在做什么错吗?

(作为附带的问题,是否有任何文件监视器可以使用JAR文件?当出现路径问题时,我通常只是打开FileMon来查看发生了什么,但是在这种情况下,它只是显示为访问JAR文件)

谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值