我正在尝试加载一个图像作为我的应用程序中的一个图标。根据这个
tutorial的适当方法是:
protected 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;
}
}
所以,我放置文件的位置,并将其作为参数传递给此函数。这没有工作,即imgURL为空。当我尝试通过明确传递路径创建ImageIcon:
ImageIcon icon = new ImageIcon(path,"My Icon Image");
它工作很棒!因此,应用程序可以从显式定义的路径中获取图像,但没有使用getResources()来获取图像。在这两种情况下,路径变量的值都相同。为什么不起作用?类加载器如何找到资源?
谢谢。