JavaFX改变标签中一个单词的颜色

时间:2021-05-19 17:00:28

I am building a property management system for desktop and I am currently working on a payment feed feature. I want the payment amount to be highlighted in a different color within my label to improve readability.

我正在为桌面构建一个属性管理系统,目前正在开发一个支付提要特性。我希望付款金额在我的标签中以不同的颜色突出显示,以提高可读性。

I have tried the following approach:

我尝试了以下方法:

String datePaid  = "just now";
Label amount = new Label("350");
Label label2 = new Label("paid £" + amount.getText() + " " + datePaid);

I then tried to apply the following CSS

然后我尝试应用以下CSS

    amount.setStyle("-fx-text-fill: #000 !important; -fx-highlight-text-fill: #000 !important; -fx-font-family: Arial");
    label2.setStyle("-fx-text-fill: #fff; -fx-font-size: 14px; -fx-translate-x: -36; -fx-translate-y: 24; -fx-font-family: 'Open Sans Light'");

I thought by declaring !important I would override the styles applied in label2, but instead all text renders to the screen in #fff

我想通过声明!重要的是,我将覆盖label2中应用的样式,但是所有的文本渲染到#fff的屏幕上

How would I go about achieving the desired result?

我怎样才能达到预期的结果呢?

1 个解决方案

#1


6  

Please try using Text inplace of Label for amount. I hope it will fix the issue. You can directly apply color to the Text as well.

请尝试使用文字代替标签的金额。我希望它能解决这个问题。你也可以直接将颜色应用到文本中。

Text amount = new Text("350");
amount.setFill(Color.RED);

#1


6  

Please try using Text inplace of Label for amount. I hope it will fix the issue. You can directly apply color to the Text as well.

请尝试使用文字代替标签的金额。我希望它能解决这个问题。你也可以直接将颜色应用到文本中。

Text amount = new Text("350");
amount.setFill(Color.RED);