ImageIcon在Java中加载 [英] ImageIcon Loading in Java

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

问题描述

我搜索了Google,SE和Oracle的文档,以获取有关如何执行此操作的信息,但我似乎无法使其正常工作。

I have searched Google, SE, and Oracle's documentation for information on how to do this, but I can't seem to make it work.

我找到了关于是否使用 getResource() getResourceAsStream()的几个帖子以及如何格式化文件名,但没有我可以申请我的情况。

I have found several posts regarding whether to use getResource() or getResourceAsStream() and how to format the file name, but none that I can apply to my situation.

我的目录结构如下(在资源文件夹中有一些额外的级别):

My directory structure looks like this (with some extra levels in the resource folder):

Root Folder
|
|_____ bin (compiled .class files go here)
|
|_____ src
|         |
|         |_____ packageA
|         |         |
|         |         |_____ packageAa
|         |                   |
|         |                   |_____ images.java
|         |                   |
|         |                   |_____ resources
|         |                             |
|         |                             |_____ icon1.gif
|         |
|         |_____ packageB
|         |
|         ... (More packages with sub-packages)
|
|_____ resources (should it go here?)
          |
          |_____ icon2.gif

在我的实际程序中,我有107个带文件名的gif图像可以从对象值派生。这些图像目前位于两个资源文件夹中,因为我一直在玩这两个图片以试图让它们工作。

In my actual program I have 107 gif images with file names that can be derived from object values. These images are currently located in both resource folders because I have been playing around with both to try to get one to work.

我当前的代码尝试这个:

My current code tries this:

String imageFileName = getFileName(obj);  // returns a string such as "/resources/cardImages/acespades.gif"
URL imageURL = getClass().getClassLoader().getResource(imageFileName);
ImageIcon icon = new ImageIcon(imageURL);

无论前导反斜杠如何,我都会收到空指针异常。

I keep getting a null pointer exception regardless of leading backslashes.

我应该如何格式化文件名和/或创建一个URL对象来获取图像?

How should I format the file name and or create a URL object to get the images?

注意:我使用Sublime Text 3和Javatar来编译我的代码。文件夹结构(bin文件夹)由Javatar创建。我的源文件夹设置为 src ,上面显示的根文件夹是我的闪存驱动器。

Note: I am using Sublime Text 3 with Javatar to compile my code. The folder structure (the bin folder) is created by Javatar. My "Source folder" is set to src and the "root folder" shown above is my flash drive.

=================================== =================

修改:

====================================================

我试过Peter的建议和结果如下:

I tried Peter's suggestions and these are the results:

URL imageURL = getClass().getResource("resources/cardImages/back1.gif");
System.out.println(imageURL);
ImageIcon icon = new ImageIcon("resources/cardImages/back1.gif");

println语句打印出null,但ImageIcon会正确显示图像。我理解这会加载.java文件旁边文件夹中的图像。

The println statement prints out null, but the ImageIcon does correctly display the image. I understand it this loads the image from the folder next to the .java file.

URL imageURL = getClass().getResource("/resources/cardImages/back1.gif");
System.out.println(imageURL);
ImageIcon icon = new ImageIcon("/resources/cardImages/back1.gif");

println仍然打印null,但现在不显示图像。我复制并粘贴了整个资源文件夹,以确保它在两个位置都相同。

The println still prints null, but now the image is not displayed. I copied and pasted the entire resource folder to make sure it was identical in both locations.

推荐答案

在课程上加载资源时,路径是相对于包路径的,除非你有一个前导斜杠/.

When loading resources over a class, the path is relative to the package path, unless you have a leading slash /.

至于你的icon1.gif,使用

As for your icon1.gif, using

URL url = getClass().getResource("resources/icon1.gif");

应返回正确的网址。

我建议将Java源和资源分开。如果resources是源文件夹(其输出文件夹是bin / JAR的根目录),请使用:

I recommend keeping the Java sources and resources separate. If resources is a source folder (whose output folder is the root of bin/the JAR), use:

URL url = getClass().getResource("/icon2.gif");

(注意前导斜线)。

编辑(问题已更新):

更正了第二个网址(资源是源文件夹,icon2.gif在它的根目录中。

我刚刚在Eclipse中验证了它,并且我得到了两个变体的正确(非空)URL。使用 ImageIcon 中的URL也会得到正确的结果,图像会显示出来。

EDIT (question updated):
Corrected the second URL (resources is a source folder, and icon2.gif lies in its root).
I just verified it in Eclipse, and I get the correct (non-null) URLs for both variants. Using the URLs in ImageIcons also gives the correct results, the images are shown.


  • 确保资源是源文件夹。并非所有项目文件都位于输出文件夹中(通常是 bin )以及稍后的JAR。

  • 检查输出文件夹以验证它是否包含已编译的类以及资源。

  • Make sure that resources is a source folder. Not all project files land in the output folder (usually bin) and later the JAR.
  • Check your output folder to verify it contains the compiled classes as well as the resources.

这篇关于ImageIcon在Java中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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