【问题标题】:Dynamically add CSS stylesheets in JavaFX在 JavaFX 中动态添加 CSS 样式表
【发布时间】:2013-04-20 15:21:17
【问题描述】:

我想添加一个位于文件系统某处的 CSS 文件。 目的是编写一个应用程序,用户可以在其中动态添加 JavaFX CSS 文件(由任何人创建并位于任何地方)。
我尝试了类似的方法,仅用于测试,以查看动态添加的 CSS 文件是否有效:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Label label = new Label("Hello");
        Scene scene = new Scene(label);

        //file would be set by an file chosser
        File file = new File("C:/test.css");
        scene.getStylesheets().add(file.getAbsolutePath());

        primaryStage.setTitle("Title");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

但我总是遇到同样的错误:

WARNING: com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged Resource "C:\test.css" not found. 

我该如何解决?

【问题讨论】:

    标签: java css javafx


    【解决方案1】:

    如果 css 在同一个包中,只需使用

    scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
    

    【讨论】:

    • 如果 CSS 与类不在同一个包中?
    【解决方案2】:

    您的问题是您没有使用 URL。 Here 您可以在 CSS 参考旁边找到更多关于如何加载 CSS 的文档。

    如果您的 URL 为 String,您可以使用外部文件动态设置 CSS,如下所示:

    private boolean isANext = true;
    
    public void start(Stage primaryStage) throws Exception {
        Button button = new Button("Change CSS");
        VBox vbox = new VBox(10);
        vbox.setAlignment(Pos.CENTER);
        vbox.getChildren().add(button);
        scene = new Scene(vbox, 200, 200);
    
        button.setOnAction(ev -> {
            // Alternate two stylesheets just for this demo.
            String css = isANext ? "file:///C:/temp/a.css" : "file:///C:/temp/b.css";
            isANext = !isANext;
            System.out.println("Loading CSS at URL " + css);
    
            scene.getStylesheets().clear();
            scene.getStylesheets().add(css);
        });
    
        primaryStage.setTitle("Title");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

    a.css

    .button {    
        -fx-text-fill: white;
        -fx-background-color: red;
    }
    

    b.css

    .button {    
        -fx-text-fill: white;
        -fx-background-color: black;
    }
    

    【讨论】:

      【解决方案3】:

      您可以从java.io.File获取网址

      File file = new File("style.css");
      URL url = file.toURI().toURL();
      scene.getStylesheets().add(url.toExternalForm());
      

      或简称

      scene.getStylesheets().add((new File("style.css")).toURI().toURL().toExternalForm());
      

      【讨论】:

        【解决方案4】:

        抛出异常是因为字符串"C:/test.css" 不是URI 资源。因此,您必须将字符串转换为 URI 资源。

        从 Java 7 开始,您可以这样做:

        String uri = Paths.get("C:/test.css").toUri().toString();
        scene.getStylesheets().add(uri);
        

        【讨论】:

          【解决方案5】:
          scene.setUserAgentStylesheet("Assets/StyleSheets/Styless.css");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-02-03
            • 2015-05-01
            • 1970-01-01
            • 2015-10-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多