从无法在 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;
}
}
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为File.separator是文件的系统相关字符,对于mac来说是“/”,对于windows来说是“\”。但是,在 URL 中,所有分隔符都应为“/”。尝试将第一行更改为:
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: