显示Windows 7的ImageIcon png文件的正确途径是什么? [英] What is the correct path to display an ImageIcon png file for Windows 7?
问题描述
我想测试一个带有简单png图像的程序。我写了一个简短的程序来做到这一点,但我似乎无法让路径正确。我已经检查,再次检查,重新检查,并检查了我的路径名称四倍,但没有正确,但无论我做什么,这个图像都不会显示。我使用了Oracle在ImageIcon文档( creaetImageIcon()
)中编写的一个简短类来完成此任务,但它似乎没有帮助。我将在下面发布整个程序,因为它非常短。
I wanted to test having a program with a simple png image on it. I wrote a short program that does this, but I can't seem to get the path right. I have checked, checked again, rechecked, and quadruple checked my path name as to not get it right, but this image will not display, no matter what I do. I used a short class wrote by Oracle in the ImageIcon documentation (the creaetImageIcon()
) to accomplish this, but it doesn't seem to help. I'll post the entire program below, as it is very short.
package practiceImages;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ImageIconGUITest {
public static void main(String[] args) {
ImageIconGUITest gui = new ImageIconGUITest();
gui.display();
}
private ImageIcon createImageIcon(String path, String description) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
private void display() {
JFrame frame = new JFrame();
JLabel label = new JLabel(createImageIcon(
"Users/Evan/javaItems/Sprites_and_Other_Art/green.png", "the color green"));
frame.add(BorderLayout.CENTER, label);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
推荐答案
getResource(String)
方法只能查找应用程序的运行时类路径上的资源。由于此图像看起来像应用程序资源(即由您作为应用程序的一部分提供),因此应放在运行时类路径上。
The getResource(String)
method will only find resources that are on the run-time class-path of the application. Since this image seems like an application resource (i.e. supplied by you as part of the application) it should be put on the run-time class-path.
EG大多数IDE都有一个可以在项目结构中放置资源的地方,这些资源将在运行时自动包含在内。将图像移动(或复制)到该路径。
E.G. Most IDEs have a place you can put resources within the project structure, that will automatically be included at run-time. Move (or copy) the image to that path.
然后,提供正确的字符串
就成了问题。让我们假设您的项目设置如下:
Then it becomes a matter of providing the correct String
. Let us imagine your project is set up something like this:
- bin
- src
- bin
- src
- com
- 我们的
- 我们的
- com
- our
- Application.java
- green.png
所以 Application.java
在包com.our;
中,而图像位于路径 resources / green.png
。
So Application.java
is in package com.our;
, while the image is in the path resources/green.png
.
如果从申请
,正确的路径是(请滚筒滚动..)
If accessing the image from the Application
, the correct path would be (drum roll please..)
/ resources / green.png
- 领先
/
很重要。它告诉JRE我们想要从'类路径的根'查找图像,而不是使用相对于类本身的包的路径。 - 正确的情况也很重要。一串
/ resources / green.png
将 不 找到名为的图片/resources/Green.png
或/ resources / green.PNG
。
- The leading
/
is important. It tells the JRE we want to look for the image from the 'root of the class-path', as opposed to using a path relative to the package of the class itself. - Correct case is also vital. A string of
"/resources/green.png"
will not locate an image named"/resources/Green.png"
or"/resources/green.PNG"
.
Eclipse路径
Eclipse paths
- 对单击
src
目录,选择菜单底部的属性
。
- 导航(使用你没有使用Eclipse的正常方法)到
Location
的目录。
- 然后转到父目录。
- 您应该看到一个
bin
目录,其中包含类和(希望)图像。
- Right click on the
src
directory, selectProperties
at the bottom of the menu.
- Navigate (using the normal way you'd use without Eclipse) to the directory of the
Location
. - Then go to the parent directory.
- You should see a
bin
directory that contains classes and (hopefully) the image.
这篇关于显示Windows 7的ImageIcon png文件的正确途径是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!