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

Java 如何为尚未完全定义的新标签赋予CSS属性?,java,javafx,attributes,grid-layout,gridpane,Java,Javafx,Attributes,Grid Layout,Gridpane,我有一个系统,它在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 li

我有一个系统,它在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
我当前的代码: countsend响应++ 添加(新标签(messageSend),0,countSendResponces)


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

要更改JavaFX中任何节点的CSS样式,需要使用
node.setStyle(“//在此处插入CSS”)

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

Node.setStyle("
   -fx-background-color: red;
");
有关在JavaFX中使用CSS的完整参考指南,请访问:


请记住,在JavaFX中使用CSS时,始终必须以
-fx-

messageSend开头的每个属性都等于我要发送的消息。我认为这不需要任何代码来解释,因为它只是放在标签中的字符串。如果需要,我会给出相应的代码。在添加/修改它之前,只需将
标签
存储在局部变量中。