Mac 上 Eclipse src 文件夹中图像文件的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17398533/
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-09-19 20:49:10  来源:igfitidea点击:

Path for image file in Eclipse src folder, on a Mac

javaeclipsemacospath

提问by Vons

I'm trying to load an image file.

我正在尝试加载图像文件。

File imageCheck = new File("Users/me/Documents/workspace/ChineseChess/src/RCar.gif");
if (imageCheck.exists())
    System.out.println("Image file found!");
else
    System.out.println("Image file not found! :(");
button.setIcon(new ImageIcon("Users/me/Documents/workspace/ChineseChess/src/RCar.gif"));
  1. Should I put RCar.gif in the src or bin folder of an eclipse project?
  2. How would I refer to it without the explicit path? (I'm using a mac. I believe it should be something like ~/RCar.gif but am having trouble finding the correct syntax online.
  1. 我应该将 RCar.gif 放在 eclipse 项目的 src 或 bin 文件夹中吗?
  2. 如果没有显式路径,我将如何引用它?(我使用的是 mac。我相信它应该类似于 ~/RCar.gif 但我在网上找不到正确的语法。

Thanks!

谢谢!

回答by MadProgrammer

If I recall correctly, Eclipse requires all resources to be placed in the resources directory. These will be automatically bundled with your application when you export it.

如果我没记错的话,Eclipse 要求所有资源都放在资源目录中。这些将在您导出时自动与您的应用程序捆绑在一起。

At runtime, you would simply use Class#getResource("/path/to/your/resource")where the path is the location from the "resource" folder

在运行时,您只需使用Class#getResource("/path/to/your/resource")路径是“资源”文件夹中的位置

回答by Alex Gittemeier

If you want to use relative paths, they are going to be relative to the project's root, or when its deployed, the directory that the jar file resides in. You can use src/RCar.gifas your filename.

如果您想使用相对路径,它们将相对于项目的根目录,或者在部署时,jar 文件所在的目录。您可以将其src/RCar.gif用作文件名。

It would be better if you created a separate folder for resources, then address them with new File("res/RCar.gif");Another option is to put them in the src folder and address them using the classloader.

如果您为资源创建了一个单独的文件夹,然后使用new File("res/RCar.gif");另一种选择将它们放在 src 文件夹中并使用类加载器对它们进行寻址,那就更好了。

回答by Sandro

You can load the icon quite easily when it is in the resource folder of the class it belongs to at runtime. See http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource%28java.lang.String%29

当图标在运行时位于它所属类的资源文件夹中时,您可以很容易地加载它。见http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource%28java.lang.String%29

Within the class your.packagename.YourClassNameyou could write the following:

在类中,your.packagename.YourClassName您可以编写以下内容:

String folderName = YourClassName.class.getName().replace('.', '/' );
String urlString = "/"+folderName+"/RCar.gif";
URL fileUri = YourClassName.class.getResource(urlString);
button.setIcon(new ImageIcon(fileUri));

The urlString would be /your/packagename/YourClassName/RCar.gif. Of course this is also where icon should be at runtime.

urlString 将是/your/packagename/YourClassName/RCar.gif. 当然,这也是图标在运行时应该出现的位置。

HTH

HTH

回答by Chinmoy

yes...it takes the relative path.

是的...它需要相对路径。

public static final String FILE_PATH = "src/test/resources/car.png";

public static final String FILE_PATH = "src/test/resources/car.png";