将资源(图像、声音等)嵌入到 Java 项目中,然后使用这些资源

发布于 2024-09-19 11:32:22 字数 1228 浏览 3 评论 0原文

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

我知道这可以在 Visual Studio 世界中完成,但我非常不熟悉如何在 Java/Eclipse IDE 中完成此操作。作为一个附带问题,如何让 Eclipse 将项目创建为可执行文件以便可以启动它?

非常感谢任何帮助。

马克

更新1:

根据BalusC的回复,我想分享我必须解决我的问题的代码。我的类位于“Viking.Test”包下,然后我将图像文件放在“Viking.Test.Resources”包下。这一切都是在 Eclipse 中完成的,以便将图像导入到项目中。

  1. 我通过右键单击“Project ->”导入图像导入->导入源的常规/文件系统。
  2. 选择了包含要导入的图像的文件夹
  3. “进入文件夹”参数选择了“Project/src/Viking/Test/Resources
  4. 未更改任何选项并单击“完成”

在源文件中,我添加了以下代码以将图像插入到 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 ) {  }

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.

  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"

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花之痕靓丽 2024-09-26 11:32:22

只需将这些资源放入源/包结构中并使用 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 文件

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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文