如何从Java资源中加载图标? [英] How to load icon from resource in Java?

查看:54
本文介绍了如何从Java资源中加载图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复项(已解决): https://stackoverflow.com/a/1133132/783469

我的应用程序有图标(jpg,png),存储在我的目录/var/tmp/gameXbox/src/image/< here> 中.现在,如何在应用程序中使用它们,而不使用硬链接而是将其用作资源?

I have icons (jpg, png) for my application, which is stored in my directory /var/tmp/gameXbox/src/image/<here>. Now, How can i use them in application, without using hard link but as resource?

示例:不起作用

IconForMyButton = ImageIO.read(new File(
                    ClassLoader.getSystemResourceAsStream("image/button1.png")
                  ));

在使用硬链接时有效:

IconForMyButton = ImageIO.read(new File(
                      "/var/tmp/gameXbox/src/image/button1.png"
                  ));

推荐答案

资源加载相对于当前包在类路径中进行.如果/var/tmp/gameXbox/src/在您的类路径中,则:

Resource loading takes place in the classpath, relative to the current package. If /var/tmp/gameXbox/src/ is in your classpath, then:

ImageIO.read( ClassLoader.getSystemResource( "image/button1.png" ) );

但是,IDE通常将 src 文件夹不包含在类路径中.尝试将图像添加到 bin 文件夹.

However, usually the src folder is not included in the classpath by IDEs. Try adding the image to the bin folder.

这篇关于如何从Java资源中加载图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆