从无法在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;
    }
}
    
已邀请:
        因为File.separator是文件的系统相关字符,所以对于Mac,它是\“ / \”,对于Windows,它是\“ \\\”。但是,在URL中,所有分隔符都应为\'/ \'。尝试将第一行更改为:
public final static String PATH = \"resources/\";
    

要回复问题请先登录注册