【问题标题】:getStylesheets().add(...) not working with Windows (7)getStylesheets().add(...) 不适用于 Windows (7)
【发布时间】: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


【解决方案1】:

user.dir 只是程序启动的当前目录。放一个 .说它是相对于当前目录的路径。然后将其转换为正确的字符串,如图所示。

File f = new File("./resources/style/stylesheet.css");
scene.getStylesheets().add(f.toURI().toURL().toExternalForm());

补充:我查了user.dir的定义,java人不使用术语current dir。相反,他们说

"user.dir" 用户工作目录

【讨论】:

    【解决方案2】:

    在 Windows 上,创建“文件”URI 的适当方法是在驱动器号之前添加三个斜杠:C:\something\something 变为 file:///C:/something/something

    更好的是,由于您使用的是Path,因此只需使用Path.toUri()。它会在 Linux 和 Windows 上为您正确执行此操作。

    【讨论】:

    • 感谢您的回答。我尝试了您的建议,但没有成功。使用Path.toUri() 需要.toString() 来适应add(...) 并产生抛出相同异常的字符串file:///C:\something\somethinig\...。 Linux 工作正常。
    • @parboiledRice 你能把你做的代码添加到你的问题中吗?我不确定路径的所有部分都正确连接。另外,您确定该文件确实存在吗?
    【解决方案3】:

    不确定您的开发设置,但我通常从 IDE 创建的 JAR 文件中获取资源。如果您使用的是 fxml,则可以从作为参数传递给 start() 方法的 URL 开始。然后我使用如下内容:

            private String getCssFileLocation() {
                String locInJar = "/net/clarkonium/jist/resources/css/help.css";
                System.out.println("fxml URL: " + fxmlUrl);
                String externalForm = fxmlUrl.toExternalForm();
                System.out.println("fxmUrl.toExternalForm(): " + externalForm);
                String prefix = externalForm.substring(0, externalForm.indexOf("!") + 1);
                System.out.println("prefix: " + prefix);
                String resLocation = prefix + locInJar;
                System.out.println("resLocation: " + resLocation);
                return resLocation;
            }
    

    由于我的程序知道它想要的资源的位置,它会将其粘贴到 URL 上以代替 fxml 位置。程序的运行产生了这个输出,显示了函数所采取的步骤:

    fxml URL: jar:file:/C:/projects/Jist/dist/run786853897/Jist.jar!/net/clarkonium/jist/RepositorySetup.fxml
    fxmUrl.toExternalForm(): jar:file:/C:/projects/Jist/dist/run786853897/Jist.jar!/net/clarkonium/jist/RepositorySetup.fxml
    prefix: jar:file:/C:/projects/Jist/dist/run786853897/Jist.jar!
    resLocation: jar:file:/C:/projects/Jist/dist/run786853897/Jist.jar!/net/clarkonium/jist/resources/css/help.css
    

    然后就可以使用返回的位置来添加样式表了。

    感叹号之前的所有内容都取决于我的 IDE,并且会随着运行而变化。此方法在开发完成并从其他位置运行程序后也有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多