将资源(图像、声音位等)嵌入到 Java 项目中,然后使用这些资源 [英] Embedding resources (images, sound bits, etc) into a Java project then use those resources
问题描述
我搜索了一种在 Java 项目中嵌入资源的方法(使用 Eclipse v3.6.0),然后在控件中使用该嵌入资源(例如,JLabel
).我见过从文件系统中引用资源的方法.项目开发完成后,我想将应用程序发布为可执行文件.应该注意的是,这些可执行文件将部署/启动到 Windows、*NIX 和 Linux 平台.
我知道这可以在 Visual Studio 世界中完成,但我非常不熟悉如何在 Java/Eclipse IDE 中执行此操作.作为一个附带问题,我如何让 Eclipse 将项目创建为可执行文件以便它可以启动?
非常感谢任何帮助.
标记
更新 1:
根据BalusC 的回复,我想分享我必须解决的问题的代码.我的课程在Viking.Test
"包下,然后我将图像文件放在Viking.Test.Resources
"包下.这一切都在 Eclipse 中完成,以将图像导入到项目中.
- 我通过右键单击导入源的项目 -> 导入 -> 常规/文件系统来导入图像.
- 选择包含要导入的图像的文件夹
- 为'进入文件夹'参数选择
Project/src/Viking/Test/Resources
" - 没有更改任何选项并点击完成"
在源文件中,我添加了以下代码以将图像插入JLabel
(LblLogo
)
试试{ClassLoader classLoader = Thread.currentThread().getContextClassLoader();InputStream 输入 = classLoader.getResourceAsStream("Viking/Test/Resources/MyImage.jpg");图像标志 = ImageIO.read(input);LblLogo = new JLabel( new ImageIcon( logo ) );LblLogo.setBounds(20, 11, 210, 93);getContentPane().add(LblLogo);}捕捉(IOException e){}
只需将那些资源放在源/包结构中并使用 ClassLoader#getResource()
或 getResourceAsStream()
通过完全限定的包路径从类路径中以 URL
或 InputStream
的形式获取它们.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();InputStream input = classLoader.getResourceAsStream("/image.gif");//...
或者如果和当前类在同一个包中,也可以通过如下方式获取:
InputStream input = getClass().getResourceAsStream("image.gif");
<小时><块引用>
作为一个附带问题,我如何让 Eclipse 将项目创建为可执行文件,以便它可以启动.
右键单击 Java 项目 > 导出 > 可运行的 JAR 文件.
I have searched for a method of embedding a resource in a java project (using Eclipse v3.6.0) then using that embedded resource within a control (e.g., JLabel
). I have seen methods of referencing resources from the file system. Once the project has been developed I would like to publish the application as an executable. It should be noted these executables will be deployed/launched to Windows, *NIX, and Linux platforms.
I know this can be done in the Visual Studio world, but I'm very unfamiliar how to do this in Java/Eclipse IDE. As a side question, how do I get Eclipse to create the project as a executable so it can be launched?
Any help is greatly appreciated.
Mark
UPDATE 1:
Based upon BalusC's response, I wanted to share the code I have to resolve my problem. My classes are under the package of "Viking.Test
" and then I placed the image file under the package "Viking.Test.Resources
". This is all done within Eclipse to import the image into the project.
- I imported the image by right-clicking on the Project -> Import -> General/File System for the import source.
- Selected the folder which contained the image to import
- Selected "
Project/src/Viking/Test/Resources
" for the 'Into folder' parameter - Didn't change any of the options and clicked "Finished"
In the source file I added the following code to insert the image into a JLabel
(LblLogo
)
try
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream(
"Viking/Test/Resources/MyImage.jpg");
Image logo = ImageIO.read(input);
LblLogo = new JLabel( new ImageIcon( logo ) );
LblLogo.setBounds(20, 11, 210, 93);
getContentPane().add(LblLogo);
}
catch ( IOException e ) { }
Just put those resources in the source/package structure and use ClassLoader#getResource()
or getResourceAsStream()
to obtain them as URL
or InputStream
from the classpath by the full qualified package path.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("/image.gif");
// ...
Or if it is in the same package as the current class, you can also obtain it as follows:
InputStream input = getClass().getResourceAsStream("image.gif");
As a side question, how do I get Eclipse to create the project as a executable so it can be launched.
Rightclick Java Project > Export > Runnable JAR File .
这篇关于将资源(图像、声音位等)嵌入到 Java 项目中,然后使用这些资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!