如何为尚未完全定义的新标签提供CSS属性?

问题描述 投票:-2回答:1

我有一个系统,它在Javafx中创建一个预定义的新Label,但只要在运行方法时就在现场创建。我希望能够编辑创建的标签的背景颜色。

countSendResponses++
mainLayout.add(new Label(messageSend), 0, countSendResponces).Color.rgb(1, 1, 1);
// that gives an error, I have tried the same thing in different places in that line of code. Nothing works

我当前的代码:countSendResponses ++ mainLayout.add(new Label(messageSend),0,countSendResponces);

有没有办法做到这一点?如果有更好的方法来做我正在做的事情,我愿意接受建议。我希望能够更改标签的背景颜色。谢谢!

java javafx attributes grid-layout gridpane
1个回答
0
投票

要更改JavaFX中任何节点的CSS样式,您需要使用Node.setStyle("//Insert CSS Here.");

例如,要更改文本的背景颜色,您可以使用:

Node.setStyle("
   -fx-background-color: red;
");

有关在JavaFX中使用CSS的完整参考指南,您可以访问:

https://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html#typecolor

请记住,在JavaFX中使用CSS时,您始终必须使用-fx-启动每个属性。

© www.soinside.com 2019 - 2024. All rights reserved.