简体   繁体   English

如何访问Eclipse项目文件夹中的图像文件

[英]How to access a Image file in a Eclipse Project Folder

I have a single image file in a folder in my Eclipse project that stores a image file (logo.jpg). 我在Eclipse项目的文件夹中有一个图像文件,用于存储图像文件(logo.jpg)。 However I don't know how to access it in my main program. 但是我不知道如何在我的主程序中访问它。

I had tried the following 我曾尝试过以下方法

private static URL logoPath

public class MainApplication
{
     public void createGui()
     {
          logoPath.getClass().getResource("/Resources/images/logo.jpg");
          ////
     }
     /////
}

Problem is I keep getting a null pointer exception so obviously that path is done wrong or else the logoPath.getClass() is tripping it up. 问题是我一直得到一个空指针异常,所以很明显路径做错了,否则logoPath.getClass()就会绊倒它。

Any ideas? 有任何想法吗?

U can use this way I have following package structure 你可以用这种方式我有以下的包结构

  • src/test (package test contain Java files) src / test(包测试包含Java文件)

  • src/images (folder images contain images) src / images(文件夹图片包含图片)

I am going to get image from src/images/login.png at src/test/*.java 我将在src / test / *。java中src / images / login.png获取图像

JLabel label = new JLabel(new ImageIcon(getClass().getResource("/images/login.png")));

You need to place your resources in a java source directory for them to be visible. 您需要将资源放在Java源目录中以使其可见。 You have two options: 您有两种选择:

  1. Move your "resources" folder under your existing "src" folder. 将“resources”文件夹移动到现有“src”文件夹下。

  2. Create a new source folder just for resources. 仅为资源创建新的源文件夹。 You can do that in Java Build Path page of project properties. 您可以在项目属性的Java Build Path页面中执行此操作。

Several things to watch out for... 有几点需要注意......

  1. Pay attention to your capitalization. 注意你的大写。 Java resource lookup is case sensitive. Java资源查找区分大小写。

  2. The path that you would use will be relative to the source folder (not project root). 您将使用的路径将相对于源文件夹(而不是项目根目录)。 For instance, if you make your resources folder a source folder, your path will need to be "images/...". 例如,如果您将资源文件夹设置为源文件夹,则您的路径将需要为“images / ...”。 If you want to preserve resources folder in the lookup path, you will need to create an extra folder level in your project to serve as the source root for resources. 如果要在查找路径中保留资源文件夹,则需要在项目中创建额外的文件夹级别,以充当资源的源根。

  3. I am not certain whether it is an actual problem, but resources paths should not start with a leading slash. 我不确定它是否是一个实际问题,但资源路径不应该以前导斜杠开头。 They aren't really paths in a traditional sense. 它们不是传统意义上的真正路径。 Think of them as package-qualified class names, but with '/' instead of '.' 将它们视为包限定的类名,但使用'/'而不是'。' as the separator. 作为分隔符。

I am doing that the same way 我也是这样做的

 String filename = "logo.jpg";

 Main.getClass().getClassLoader().getResource(filename);

And the file structure looks like this 文件结构如下所示

/src/main/java/com/hauke/Main.java /src/main/java/com/hauke/Main.java

/resource/logo.jpg /resource/logo.jpg

My problem was before that I named the direcotry "resources" and it should be "resource" 我的问题是在此之前我命名为“资源”,它应该是“资源”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM