开发者

Load image from resource not working on windows

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜