Make a dark mode with JavaFx

后端 未结 4 1019
自闭症患者
自闭症患者 2021-02-06 11:35

I was wondering if there is an easy way to make a dark mode using JavaFx and CSS. I have a MenuBar with a CheckMenuItem called \'Dark mode\' and when I click it I want the scene

4条回答
  •  不知归路
    2021-02-06 11:57

    the property base can be applied to every JavaFX type, This enables a color theme to be specified using a single base color for a JavaFx Node or Layout..., and to have variant colors (for its children) computed based on that base color!

    in this case, you are trying to set the theme for the whole scene so you should apply the base color to the highest Component in the hierarchy which you can get by getting the root Node of your scene!

    checkMenuItem.selectedProperty().addListener((obs, wasSelected, isSelected) -> {
        if (isSelected) {
            scene.getRoot().setStyle("-fx-base:black");
        } else {
            scene.getRoot().setStyle("");
        }
    });
    

提交回复
热议问题