为 Windows 7 显示 ImageIcon png 文件的正确路径是什么? [英] What is the correct path to display an ImageIcon png file for Windows 7?

查看:26
本文介绍了为 Windows 7 显示 ImageIcon png 文件的正确路径是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一个带有简单 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.

E.G.大多数 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.

然后就需要提供正确的String.让我们假设您的项目是这样设置的:

Then it becomes a matter of providing the correct String. Let us imagine your project is set up something like this:

  • 源代码
  1. com
    • 我们的
  1. com
    • our
  1. 应用程序.java

  • green.png

所以 Application.javapackage com.our; 中,而图像在路径 resources/green.png 中.

So Application.java is in package com.our;, while the image is in the path resources/green.png.

如果从 Application 访问图像,正确的路径应该是(请鼓点...)

If accessing the image from the Application, the correct path would be (drum roll please..)

"/resources/green.png"

  1. 开头的 / 很重要.它告诉 JRE 我们要从类路径的根目录"中查找图像,而不是使用相对于类本身的包的路径.
  2. 正确的案例也很重要.一串 "/resources/green.png"定位名为 "/resources/Green.png" "/resources/green.PNG".
  1. 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.
  2. 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 路径

  1. 右键单击src 目录,选择菜单底部的Properties.
  2. 导航(使用您在没有 Eclipse 的情况下使用的正常方式)到 Location 的目录.
  3. 然后转到父目录.
  4. 您应该会看到一个 bin 目录,其中包含类和(希望如此)图像.
  1. Right click on the src directory, select Properties at the bottom of the menu.
  2. Navigate (using the normal way you'd use without Eclipse) to the directory of the Location.
  3. Then go to the parent directory.
  4. You should see a bin directory that contains classes and (hopefully) the image.

这篇关于为 Windows 7 显示 ImageIcon png 文件的正确路径是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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