有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从不在windows上工作的资源加载映像

为什么这在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;
    }
}

共 (1) 个答案

  1. # 1 楼答案

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

    public final static String PATH = "resources/";