所以,我有一个动态创建的 TextArea
列表,我想根据我选择的 CheckBox
来绘制它们。目前,它只为边框着色,而不是 TextArea
的内部。
这是我的代码:
if (cb.isSelected()) {
ta.setStyle("-fx-background-color:#38ee00;");
}
if (cb2.isSelected()) {
ta.setStyle("-fx-background-color:orangered;");
}
if (!cb2.isSelected() && !cb.isSelected()) {
ta.setStyle("-fx-background-color:white;");
}
这是它带来的结果:
如果您需要任何其他信息,请随时告诉我。
最佳答案
您实际上需要为 .content
区域设置样式,该区域是 TextArea
( see CSS Reference: TextArea - Substructure ) 的子区域。
您可以使用lookup
获取该节点,但只能在应用皮肤之后进行,这在布局之前不会发生。然后你可以做这样的事情:
Region content = (Region) ta.lookup(".content");
content.setStyle("-fx-background-color:orangered;");
另一种方法是使用 CSS 样式表并更改 TextArea
的类(前提是只使用了有限数量的颜色):
.text-area.color-orangered .content {
-fx-background-color: orangered;
}
.text-area.color-white .content {
-fx-background-color: white;
}
.text-area.color-green .content {
...
// remove old class for coloring
ta.getStyleClass().removeIf(s -> s.startsWith("color-"));
// add new class
ta.getStyleClass().add("color-orangered");
关于带有 -fx-background-color 的 JavaFX setStyle 仅绘制 TextArea 的边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37877293/