显示Windows 7的ImageIcon png文件的正确路径是什么?

我想测试一个带有简单png图像的程序。 我写了一个简短的程序来做到这一点,但我似乎无法让路径正确。 我已经检查,再次检查,重新检查,并检查了我的路径名称四倍,但没有正确,但无论我做什么,这个图像都不会显示。 我使用了Oracle在ImageIcon文档( creaetImageIcon() )中编写的一个简短类来完成此任务,但它似乎没有帮助。 我将在下面发布整个程序,因为它很短。

 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)方法只能查找应用程序的运行时类路径上的资源。 由于此映像看起来像应用程序资源(即由您作为应用程序的一部分提供),因此应将其放在运行时类路径上。

EG大多数IDE都有一个可以将资源放在项目结构中的位置,它将在运行时自动包含在内。 将图像移动(或复制)到该路径。

然后它成为提供正确的String 。 让我们假设您的项目设置如下:

  • 箱子
  • SRC
    1. COM
      • 我们的
        1. Application.java
    2. 资源
      • green.png

所以Application.javapackage com.our; ,而图像位于path resources/green.png

如果从Application访问图像,正确的路径将是(滚筒请…)

"/resources/green.png"

笔记

  1. 领先/重要。 它告诉JRE我们想要从“类路径的根”中查找图像,而不是使用相对于类本身的包的路径。
  2. 正确的情况也很重要。 一串"/resources/green.png"将找不到名为"/resources/Green.png" "/resources/green.PNG"的图像。

Eclipse路径

  1. 右键单击src目录,选择菜单底部的Properties
    Eclipse属性
  2. 导航(使用不使用Eclipse的正常方式)到Location目录。 Eclipse属性显示项目位置
  3. 然后转到父目录。
  4. 您应该看到一个包含类和(希望)图像的bin目录。

首先,您提供了相对路径,因此系统正在查找相对于您执行程序的位置的图像。

其次,路径应该有一个驱动器规格或至少一个前导/ 。 根据您的设置,“C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png”之类的东西应该可以使用(您可能需要更改驱动器规格以满足您的系统)

第三,确保文件存在于指定位置, System.out.println(new File("C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png").exists())应该返回true ,其他方面该文件位于错误的位置。

相对路径基本上是指相对于程序执行的路径位置。 因此,如果您从C:/Program Files/MyAwesomeApplication运行该程序,则Users/Evan/javaItems/Sprites_and_Other_Art/green.png的相对路径将成为C:/Program Files/MyAwesomeApplication/Users/Evan/javaItems/Sprites_and_Other_Art/green.png的绝对路径C:/Program Files/MyAwesomeApplication/Users/Evan/javaItems/Sprites_and_Other_Art/green.png 。 这描述了从根位置到相关文件/文件夹的路径。

您可以使用System.out.println(new File("C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png").getAbsolutePath(‌​))来测试这一点,它将为您提供完整路径。

用这个修复它给我:

 JButton btnBanana = new JButton("New button"); btnBanana.setIcon(new ImageIcon("D:\\Android\\Company\\images\\bananas-icon.png")); 

使用双斜线而不是一个,我有这个问题,我修复它。 生病了给你一个例子:

public Driver(){

  ImageIcon us = new ImageIcon("C:\saeed.gif"); // OS cant find it ImageIcon uk = new ImageIcon("C:\\saeed0.gif"); // OS can JButton button = new JButton ("Click here " , us ) ; button.setRolloverIcon(uk); add(button); } 

要将图像的路径提供给文本字段,此代码将为您提供帮助

 txtPath.setText(lblImage.getIcon().toString()); 

// txtPath是文本字段使用todiplay路径// lblImage是显示图像的标签

你需要做C:\\Test\\test.png而不是C:/Test/test.png