将资源(图像,声音等)嵌入到Java项目中,然后使用这些资源 [英] Embedding resources (images, sound bits, etc) into a Java project then use those resources

查看:252
本文介绍了将资源(图像,声音等)嵌入到Java项目中,然后使用这些资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了在java项目(使用Eclipse v3.6.0)中嵌入资源的方法,然后使用控件中的嵌入式资源(例如, JLabel ) 。我已经看到了从文件系统引用资源的方法。一旦开发了项目,我想将应用程序作为可执行文件发布。应该注意,这些可执行文件将被部署/启动到Windows,* NIX和Linux平台。

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.

我知道这可以在Visual Studio世界中完成,但是在Java / Eclipse IDE中如何做到这一点非常陌生。作为一个侧面问题,如何让Eclipse将该项目创建为可执行文件,以便可以启动?

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?

非常感谢任何帮助。

标记

更新1:

根据 BalusC的回复,我想分享我必须解决我的问题的代码。我的课程包含 Viking.Test 的包,然后我将图像文件放在$ codeViking.Test.Resources 。这一切都在Eclipse中完成,将图像导入到项目中。

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.


  1. 我通过右键单击项目导入图像

  2. 选择包含导入图像的文件夹

  3. 已选择 c code code code code code code code $不要更改任何选项,点击完成

  1. I imported the image by right-clicking on the Project -> Import -> General/File System for the import source.
  2. Selected the folder which contained the image to import
  3. Selected "Project/src/Viking/Test/Resources" for the 'Into folder' parameter
  4. Didn't change any of the options and clicked "Finished"

在源文件中,我添加了以下内容将图像插入 JLabel LblLogo

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 ) {  }


推荐答案

只需将这些资源放在源码/包结构中,然后使用 ClassLoader#getResource() getResourceAsStream() 通过完全限定的包路径从类路径中获取它们作为 URL InputStream

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");








问题是,如何让Eclipse将项目创建为可执行文件,以便可以启动。

As a side question, how do I get Eclipse to create the project as a executable so it can be launched.

右键单击Java项目>导出> Runnable JAR文件

这篇关于将资源(图像,声音等)嵌入到Java项目中,然后使用这些资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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