在 javafx 中更改按钮 textFill 属性

Loc*_*oMH 0 javafx button

使用 javafx,我有一个按钮,在 css 属性中将 textFill 设置为白色。然后在运行时需要更改它,这在以下代码中发生:

import javafx.scene.control.Button;
import javafx.scene.paint.Color;

public class QAButton extends Button {

    public QAButton(String text) {
        this.textProperty().set(text);
    }

    public void setAnswerVisible(Boolean vis) {

        if (vis) this.setTextFill(Color.GREEN);
        else this.setTextFill(Color.BLUE);
    }

}
Run Code Online (Sandbox Code Playgroud)

但是它不会做任何事情,textFill 将保持白色。在运行时我可以做些什么来改变它?谢谢你。

编辑:我应该添加 css 代码:

.button {
    -fx-text-fill: white;
}
Run Code Online (Sandbox Code Playgroud)

并且按钮的 id 设置为

#question {
    -fx-background-color: darkblue;
    -fx-background-radius: 0;
    -fx-max-width: 2000px;
    -fx-max-height: 200px;
    -fx-min-height: 80px;
}
Run Code Online (Sandbox Code Playgroud)

小智 5

摘自JavaFX CSS Reference(第二部分):

“该实现允许设计人员通过使用样式表覆盖从代码设置的属性值来设计应用程序的样式。”

使用setStyle API能够解决您的问题。

例子:

this.setStyle("-fx-text-fill: green");
Run Code Online (Sandbox Code Playgroud)

现在样式类中的所有属性都将被覆盖。如果这是一个问题,请看这里