从无法在 Windows 上运行的资源加载图像

发布于 2024-11-14 18:32:41 字数 519 浏览 6 评论 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;
    }
}

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.getResource(PATH + name);
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        System.err.println("Couldn't find file: " + PATH + name);
        return null;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上青苔 2024-11-21 18:32:41

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

public final static String PATH = "resources/";

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/";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文