java 样式化 javafx 复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43566587/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:33:17  来源:igfitidea点击:

Styling javafx checkbox

javajavafx

提问by Suemayah Eldursi

I've tried to apply the following styles to my CheckBox

我尝试将以下样式应用于我的 CheckBox

.check-box .box {
    -fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, -fx-white;
}

.check-box:selected .mark {
    -fx-background-color: -fx-colour-white;
}

.check-box:selected .box {
    -fx-background-color: -fx-blue;
}

The goal is to have a white checkbox with grey border and when the checkbox is checked to have a blue background and white tick. When I apply these styles I get the result I want. However an odd thing happens when doing the following steps:
1- click on checkbox to select
2- click on checkbox to deselect
The checkbox disappears and will only appear again if I click on somewhere else in the pane.

目标是有一个带灰色边框的白色复选框,当复选框被选中时具有蓝色背景和白色勾号。当我应用这些样式时,我得到了我想要的结果。然而,在执行以下步骤时会发生奇怪的事情:
1- 单击复选框以选择
2- 单击复选框以取消选择
复选框消失,只有在我单击窗格中的其他位置时才会再次出现。

You can see the images below of the checkbox when it is first rendered then when it is checked.

您可以在第一次渲染时看到复选框下方的图像,然后在选中时看到。

Checkbox in normal stateSelected CheckboxDeselected Checkbox

正常状态下的复选框选中的复选框取消选中的复选框

回答by Bo Halim

I do not think that the problem comes from your StyleSheets, I think it's due to a bad manipulation in your Java code, you said :

我不认为问题来自您的StyleSheets,我认为这是由于您的Java 代码操作不当,您说:

The checkbox disappears and will only appear again if I click on somewhere else in the pane

复选框消失,只有在我点击窗格中的其他地方时才会再次出现

So start with your Pane, look at whether there is a related event, and concerning the style, your method is almostcorrect, this is how I would have proceeded :

所以从你的Pane开始,看看是否有相关的事件,关于样式,你的方法几乎是正确的,这就是我将如何进行的:

.check-box .box {

-fx-background-color: white;
-fx-border-color:grey;
-fx-border-radius:3px; 

}

.check-box:selected .mark {

-fx-background-color: white;

}

.check-box:selected .box {

-fx-background-color: #28a3f4;
}

Good luck !

祝你好运 !