【问题标题】:JavaFX with FXML change css file runtime, with menu-items带有 FXML 的 JavaFX 更改 css 文件运行时,带有菜单项
【发布时间】:2015-03-20 13:29:24
【问题描述】:

我正在学习 JavaFx,我正在尝试在运行时更改我的窗口(Mastermind 游戏)的 css 样式。

目前它适用于一些对象,如按钮、背景、菜单栏、标签和形状。 但我想更改的另一个对象是菜单项(背景和标签)。

我尝试使用下一个 css 代码来访问它:

.context-menu {
    -fx-background-color: linear-gradient(rgba(200,200,200,1),rgba(50,50,50,1) 80%);
}

它在初始化时起作用,但在运行时更改时不起作用。 例如,它适用于菜单栏本身:

.menu-bar {
    -fx-background-color: black;
    -fx-selection-bar: #505050;
}
.menu-bar .label {
    -fx-font-size: 12pt;
    -fx-font-family: "Segoe UI Light";
    -fx-text-fill: white;
    -fx-opacity: 0.9;
}

在 FXMLcontroller 中,我调用以下代码来更改皮肤:

@FXML private AnchorPane generalPane;
private final String themeNormal = getClass().getResource("/mastermindStyles/MasterMind_Normal.css").toExternalForm();

@FXML
void handleSkinNormal(ActionEvent event){
   generalPane.getStylesheets().clear();
   generalPane.getStylesheets().add(themeNormal);
}

我该怎么做?

【问题讨论】:

    标签: css javafx runtime menuitem fxml


    【解决方案1】:

    我找到了:

    使用预定义样式(如 Modena 和 Caspian):

    Application.setUserAgentStylesheet(STYLESHEET_MODENA);
    

    使用你自己的 CSS 样式表:

    Application.setUserAgentStylesheet(null);
    StyleManager.getInstance().addUserAgentStylesheet(
      getClass().getResource("NewSkin.css").toExternalForm());
    

    【讨论】:

      猜你喜欢
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多