我在 java 1.8.0_25 中遇到 StyleManager 问题。如果我直接将 css 文件应用到场景中,那么 css 将正确显示。但是,如果我尝试使用 StyleManager 在所有场景中应用 css 文件,则不会应用 css。
这是一个演示该问题的简单程序
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Application.setUserAgentStylesheet(null);
Parent node = new FXMLLoader(getClass().getResource("Testing.fxml")).load();
Scene scene = new Scene(node);
// StyleManager.getInstance().addUserAgentStylesheet("TabPaneTest.css");
scene.getStylesheets().add("TabPaneTest.css");
primaryStage.setScene(scene);
primaryStage.show();
}
}
测试.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Untitled Tab 1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab text="Untitled Tab 2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</VBox>
TabPaneTest.css:
.tab-pane .tab {
-fx-padding: 3 6 1 6;
-fx-border-style: none;
-fx-border-color: transparent;
-fx-border-width: 0;
-fx-border-insets: 2;
-fx-background-radius: 3 3 0 0;
-fx-background-insets: 0 2 0 2;
-fx-background-color: linear-gradient(green 80%, blue 100%);
}
如果不使用 StyleManager,它应该是这样的:
但是,当我注释掉将 css 直接应用到场景并使用 StyleManager 时,我得到的是:
有人在使用 StyleManager 时遇到过这个问题吗?不幸的是,我们有一个大型应用程序,它使用 StyleManager 将 CSS 应用到所有场景
最佳答案
在 primaryStage.show();
之后调用 StyleManager.getInstance().addUserAgentStylesheet("TabPaneTest.css");
。
自 Java 8u40 以来我遇到了同样的问题并通过这种方式修复了它。不幸的是,我还没有找到用于添加全局样式表的公共(public) API。
关于java - StyleManager 未正确应用 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29175248/