Java 如何访问 Eclipse 项目文件夹中的图像文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9787368/
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
How to access a Image file in a Eclipse Project Folder
提问by StephenHynes
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.
我的 Eclipse 项目的文件夹中有一个图像文件,用于存储图像文件 (logo.jpg)。但是我不知道如何在我的主程序中访问它。
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?
有任何想法吗?
采纳答案by Konstantin Komissarchik
You need to place your resources in a java source directory for them to be visible. You have two options:
您需要将资源放在 java 源目录中,以便它们可见。您有两个选择:
Move your "resources" folder under your existing "src" folder.
Create a new source folder just for resources. You can do that in Java Build Path page of project properties.
将“资源”文件夹移动到现有的“src”文件夹下。
仅为资源创建一个新的源文件夹。您可以在项目属性的 Java Build Path 页面中执行此操作。
Several things to watch out for...
有几件事要注意...
Pay attention to your capitalization. Java resource lookup is case sensitive.
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.
注意你的大小写。Java 资源查找区分大小写。
您将使用的路径将相对于源文件夹(而不是项目根目录)。例如,如果您将资源文件夹设为源文件夹,则路径必须为“images/...”。如果要在查找路径中保留资源文件夹,则需要在项目中创建一个额外的文件夹级别作为资源的源根目录。
我不确定这是否是实际问题,但资源路径不应以斜杠开头。它们并不是传统意义上的真正路径。将它们视为包限定的类名,但使用 '/' 而不是 '.' 作为分隔符。
回答by Hauke
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
/资源/标志.jpg
My problem was before that I named the direcotry "resources" and it should be "resource"
我的问题是在我将目录命名为“资源”之前,它应该是“资源”
回答by Surendra Jnawali
U can use this way I have following package structure
你可以用这种方式 我有以下包结构
src/test (package test contain Java files)
src/images (folder images contain images)
src/test(包测试包含 Java 文件)
src/images(文件夹图像包含图像)
I am going to get image from src/images/login.pngat src/test/*.java
我将从src/images/login.png的src/test/*.java获取图像
JLabel label = new JLabel(new ImageIcon(getClass().getResource("/images/login.png")));
JLabel label = new JLabel(new ImageIcon(getClass().getResource("/images/login.png")));