【发布时间】:2015-10-24 03:42:26
【问题描述】:
(向下滚动查看解决方案)
我在 Linux 上使用 JavaFX 制作了一个程序。我用
scene.getStylesheets().add(
"file:" + Paths.get(System.getProperty("user.dir") + "/resources/style/stylesheet.css").toString());
将我的样式表加载到应用程序中。这适用于 Linux,但不适用于 Windows。无论我做什么,它总是说
Aug 01, 2015 5:53:42 PM com.sun.javafx.css.StyleManager loadStyleSheetUnPrivileged
Warning: Resource "file:C:\Users\win7\Desktop\resources\style\stylesheet.css" not found.
有人知道她有什么问题吗?有关键字吗?
提前谢谢你!
更新: 我试过了
scene.getStylesheets().add(
"file:///" + Paths.get(System.getProperty("user.dir") + "/resources/style/stylesheet.css").toString());
和
URI stylesheetURI = Paths.get(System.getProperty("user.dir") + "/resources/style/stylessheet.css").toUri();
scene.getStylesheets().add(stylesheetURI.toString());
和
Path stylesheetPath = Paths.get(System.getProperty("user.dir") + "/resources/style/stylessheet.css");
scene.getStylesheets().add(stylesheetPath.toUri().toString());
解决方案: 我已经使用以下代码使其适用于 Linux 和 Windows:
scene.getStylesheets().add(Paths.get("./resources/style/stylesheet.css").toAbsolutePath().toUri().toString());
在@RealSkeptic 和@brian 的帮助下。谢谢!
【问题讨论】:
-
您可以省略 ./ (不仅仅是 . 否则它将是根目录)。我只是把它显示它是当前目录。记住这一点很有用,因为如果资源文件夹比 jar 文件高一级,您可以使用 ../resources/etc。一 。是当前目录,.. 升级了
标签: java css javafx stylesheet