如何将 CSS 属性赋予尚未完全定义的新标签?

How to give CSS attributes to new labels that have not been fully defined?

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

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

我当前的代码: 计数发送响应++ mainLayout.add(新标签(消息发送), 0, countSendResponces);

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

要改变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- 开始每个属性。