0

为什么这不能在 Windows 上工作,而是在 Mac 上工作?

public final static String PATH = "resources" + File.separator;

/** Returns an ImageIcon, or null if the path was invalid. */
public static ImageIcon createImageIcon(String name, String description) {
    java.net.URL imgURL = GuiTools.class.getResource(PATH + name);
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        System.err.println("Couldn't find file: " + PATH + name);
        return null;
    }
}
4

1 回答 1

2

因为 File.separator 是文件的系统依赖字符,mac 是“/”,windows 是“\”。但是,在 URL 中,所有分隔符都应为“/”。尝试将第一行更改为:

public final static String PATH = "resources/";
于 2011-06-11T14:17:24.320 回答