Eclipse 中的 Java:我应该将要使用 getResource 加载的文件系统上的文件放在哪里?(例如 ImageIcon 的图像)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/270197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:18:05  来源:igfitidea点击:

Java in Eclipse: Where do I put files on the filesystem that I want to load using getResource? (e.g. images for an ImageIcon)

javaeclipsefilesystemsimageicon

提问by Andrew Larned

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

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

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 的功能来设置资源都很感兴趣。

采纳答案by BCunningham

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 /srcthen you can create a /src/resourcesfolder to place your images/files in. Then, within your class you do a getResource("/resources/image.png")to retrieve it.

对于 Eclipse,通常您需要做的就是在源代码目录中的某处设置一个文件夹。例如,如果包含源的目录是,/src那么您可以创建一个/src/resources文件夹来放置您的图像/文件。然后,在您的类中,您可以执行 agetResource("/resources/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.mycompanypackage with the com.mycompany.Fooclass 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.

如果您愿意,您还可以将图像/文件放在与尝试访问它的类相同的文件夹/包中(例如:将 image.pngcom.mycompanycom.mycompany.Foo需要访问它的类一起放在包中并调用getResource("image.png")),但我'我发现将图像和其他文件等资源保存在类文件夹之外的它们自己的特殊目录中更容易——它们只是更容易管理。

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 中,无论何时进行构建,此资源目录中的文件都将与已编译的类一起复制到构建目录中。

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 中打开了“自动构建”(大多数人都这样做),则此目录中的任何资源在 Eclipse 之外发生更改(即您使用图像编辑工具编辑图像),IDE 可能不会始终检测到这种变化。通常对项目文件夹进行刷新将确保文件在这些情况下在构建中得到更新。

回答by jodonnell

You can either put them in the src folder alongside your classes, or you can create a new source folder for the purpose (usually called resources), although you'll locate them identically from code.

您可以将它们放在类旁边的 src 文件夹中,也可以为此目的创建一个新的源文件夹(通常称为resources),尽管您将从代码中相同地定位它们。

Then you get at them using getResource("/com/x/y/foo.png").

然后你使用getResource("/com/x/y/foo.png").