Java 个包含图像文件的项目

Java project with image files

我正在处理一个使用图像文件的项目。存储图像文件夹的正确位置在哪里?在 src 文件夹内(由 netbeans 创建)还是在外面?此外,当我清理并构建项目时,会创建一个 dist 文件夹,其中包含 jar 文件。图像文件夹是否可以包含在 jar 中,或者如果我想 运行 另一台计算机上的程序,我必须将它们复制到与 jar 相同的文件夹中?最后,我想知道当我想从源代码中引用图像文件夹时如何使用getResource。提前致谢。

Where is the proper location to store the folder with the images? Inside src folder (created by netbeans) or outside?

答案是,这取决于。如果您想要将图像捆绑在 Jar(也称为嵌入式资源)中,那么它们应该存储在 Netbeans src 目录中。如果您希望将它们存储在外部(然后您将负责确保在部署时将它们包含在 Jar 中),那么它们应该存储在 src 目录之外。

Can the images folder be included in the jar or I have to copy them in the same folder with the jar if I want to run the program on another computer?

如果图像存储在 src 目录中,它们会自动包含,如果它们存储在外部,您将需要提供一些自定义 ant 代码来执行所需的操作。

同样,如果它们存储在外部(到 src 目录),您将对它们的管理负全部责任

Finally, I would like to know how to use the getResource when I want to refer to the image folder from the source code.

BufferedImage img = ImageIO.read(getClass().getResource("{path to images}/{image name}");

有许多 resources/examples 可用,可以扩展个人 requirements/differences 如何在不同情况下使用 getResource,但这是基本思想