本文介绍了Eclipse中的Java:使用getResource将文件放在要加载的文件系统上? (例如ImageIcon的图像)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我知道文件需要是getClass()。getResource(filename)可以找到的地方,但是我不知道这是哪里。

I know the file needs to be where the getClass().getResource(filename) can find it, but I don't know where that is.

I对文件系统本身放在哪里有兴趣,以及如何使用Eclipse的功能来设置资源。

I'm interested both in where to put the files on the filesystem itself, and how to go about using Eclipse's functionality to set up the resources.

推荐答案

对于Eclipse,通常所有您需要做的是在源代码目录中的某个位置设置文件夹。例如,如果包含你的源的目录是 / src ,那么你可以创建一个 / src / resources 文件夹来放置您的图片/文件。然后,在您的类中,您将执行 getResource(/ resources / image.png)以检索它。

For Eclipse, typically all you need to do is set up a folder somewhere within your source code directory. For instance, if the directory containing your source is /src then you can create a /src/resources folder to place your images/files in. Then, within your class you do a getResource("/resources/image.png") to retrieve it.

您还可以将图像/文件放在与尝试访问它的类相同的文件夹/包中(如果您愿意的话)(例如:将image.png放在 com.mycompany 包含需要访问它并调用 com.mycompany.Foo getResource(image.png)),但是我发现将资源(如图像和其他文件)保存在类文件夹之外的特殊目录中更容易 - 它们更容易

You can also place the image/file within the same folder/package as the class trying to access it if you wish (example: place the image.png in the com.mycompany package with the com.mycompany.Foo class that needs to access it and call getResource("image.png")), but I've found it's easier to keep resources like images and other files in their own special directory outside of the class folders -- they're just easier to manage that way.

在Eclipse中,无论何时执行构建,此资源目录中的文件将与您编译的类一起复制到构建目录中。

In Eclipse, whenever you do a build, the files within this resource directory will be copied over into your build directory along with your compiled classes.

重要的是要注意,如果您在Eclipse(如大多数人)中启用了自动构建,则此目录中的任何资源都将在Eclipse之外更改(即您可以使用图像编辑工具编辑图像),IDE可能并不总是检测到此更改。通常在项目文件夹上进行刷新将确保在这些情况下文件在构建中得到更新。

It's important to note that if you have "Build Automatically" turned on in Eclipse (as most people do) any resources in this directory that get changed outside of Eclipse (i.e. you edit an image using an image editing tool) that the IDE may not always detect this change. Usually doing a refresh on the project folder will ensure that the file gets updated in the build in these situations.

这篇关于Eclipse中的Java:使用getResource将文件放在要加载的文件系统上? (例如ImageIcon的图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:45