开发者

Load image from resource not working on windows

开发者 https://www.devze.com 2023-03-12 19:07 出处:网络
why isn\'t this working on windows but on mac? public final static String PATH = \"resources\" + File.separator;

why isn't this working on windows but on 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.getResourc开发者_开发问答e(PATH + name);
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        System.err.println("Couldn't find file: " + PATH + name);
        return null;
    }
}


Because File.separator is the system dependent character for files, which is "/" for mac but "\" for windows. However, in a URL, all of the separators should be '/'. Try changing the first line to be:

public final static String PATH = "resources/";
0

精彩评论

暂无评论...
验证码 换一张
取 消