如何访问Eclipse Project文件夹中的图像文件 [英] How to access a Image file in a Eclipse Project Folder

查看:85
本文介绍了如何访问Eclipse Project文件夹中的图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Eclipse项目的文件夹中有一个图像文件,用于存储图像文件(logo.jpg)。但是我不知道如何在主程序中访问它。

I have a single image file in a folder in my Eclipse project that stores a image file (logo.jpg). However I don't know how to access it in my main program.

我尝试了以下

private static URL logoPath

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

问题是我不断收到空指针异常,因此很显然路径执行错误,否则logoPath.getClass()将其绊倒。

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.

有任何想法吗?

推荐答案

您需要将资源放在Java源目录中才能显示。您有两个选择:

You need to place your resources in a java source directory for them to be visible. You have two options:


  1. 将 resources文件夹移至现有 src文件夹下。

  1. Move your "resources" folder under your existing "src" folder.

为资源创建一个新的源文件夹。您可以在项目属性的Java Build Path页面中做到这一点。

Create a new source folder just for resources. You can do that in Java Build Path page of project properties.

需要注意的几件事...

Several things to watch out for...


  1. 请注意大小写。 Java资源查找区分大小写。

  1. Pay attention to your capitalization. Java resource lookup is case sensitive.

您将使用的路径将相对于源文件夹(而不是项目根目录)。例如,如果将资源文件夹设为源文件夹,则路径必须为 images / ...。如果要在查找路径中保留资源文件夹,则需要在项目中创建一个额外的文件夹级别,以用作资源的源根。

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/...". 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.

我不确定这是否是实际问题,但资源路径不应以斜杠开头。它们并不是传统意义上的道路。可以将它们视为符合包要求的类名,但以 /代替。。

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.

这篇关于如何访问Eclipse Project文件夹中的图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆