java 在 JavaFX 中动态添加 CSS 样式表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16236641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamically add CSS stylesheets in JavaFX
提问by F.A
I would like to add a CSS file which is located somewhere on the filesystem.
The purpose is to write an application where the user can add JavaFX CSS files (which are created by anyone and located anywhere) dynamically.
I tried something like that, only for testing, to see if dynamically added CSS files works:
我想添加一个位于文件系统某处的 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);
}
}
But I get always the same error:
但我总是得到同样的错误:
WARNING: com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged Resource "C:\test.css" not found.
How can I fix it?
我该如何解决?
回答by jijesh Aj
If css in same packaage simply use
如果 css 在同一个包中,只需使用
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
回答by Antonio J.
Your problem is that you aren't using a URL. Hereyou can find more documentation on how the CSS is loaded alongside the CSS reference.
您的问题是您没有使用 URL。在这里您可以找到更多关于如何在 CSS 参考旁边加载 CSS 的文档。
If you have the URL as a String
you could set the CSS dynamically with an external file like so:
如果您有 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();
}
In the a.css
在里面 a.css
.button {
-fx-text-fill: white;
-fx-background-color: red;
}
And in b.css
而在 b.css
.button {
-fx-text-fill: white;
-fx-background-color: black;
}
回答by HW90
You can get the URL from java.io.File
你可以从 java.io.File
File file = new File("style.css");
URL url = file.toURI().toURL();
scene.getStylesheets().add(url.toExternalForm());
or in short form
或简写
scene.getStylesheets().add((new File("style.css")).toURI().toURL().toExternalForm());
回答by pgmank
The exception is thrown because the string "C:/test.css"
is not a URI resource. Therefore you must convert your string into a URI resource.
抛出异常是因为该字符串"C:/test.css"
不是 URI 资源。因此,您必须将字符串转换为 URI 资源。
As of Java 7 you can do:
从 Java 7 开始,您可以执行以下操作:
String uri = Paths.get("C:/test.css").toUri().toString();
scene.getStylesheets().add(uri);
回答by rucuriousyet
scene.setUserAgentStylesheet("Assets/StyleSheets/Styless.css");