【问题标题】:How to give CSS attributes to new labels that have not been fully defined?如何为尚未完全定义的新标签赋予 CSS 属性?
【发布时间】:2019-08-13 19:23:27
【问题描述】:

我有一个系统,它在 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

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

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

【问题讨论】:

  • messageSend 等于我要发送的消息。我认为不需要任何代码来解释,因为它只是放置在标签中的字符串。如果需要,我会提供该代码。
  • 在添加/修改之前将Label存储在一个局部变量中。

标签: java javafx attributes grid-layout gridpane


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-23
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 2019-09-10
    • 2015-12-08
    • 1970-01-01
    相关资源
    最近更新 更多