当 bool 值改变时改变按钮文本

Change button text when bool value changes

我有一个扩展 Button 的 class。有一个设置为 false 的静态布尔值。当此布尔值更改为 true 时,我想更改所有按钮的文本。

我尝试使用绑定功能但失败了。 :D

public class KolonaA extends Button{
    ...
    static Boolean solved = false;
    ...
    public KolonaA() {
    super();
    this.setPrefSize(size[0], size[1]);
    this.setLayoutX(xCord + buttonCount * 30);
    this.setLayoutY(yCord + buttonCount * 40);

    //something like this:
    this.textProperty().bind(solved ? "true" "false"); 
    //CHANGE TEXT OF BUTTON WHEN solved CHANGES VALUE
    ...
    }
    ...
}

我不完全确定它是否仅适用于一个按钮和一组按钮。如果要更改已发布代码的自定义按钮的文本,而不是布尔值,请使用 BooleanProperty。稍后您可以向其添加 Listener 并相应地更改按钮的文本。

public class KolonaA extends Button{
    ...
    public BooleanProperty solved = new SimpleBooleanProperty();
    ...
    public KolonaA() {
        super();
        solved.addListener((observable, oldValue, newValue) -> {
                if(newValue)
                    setText("True");
                else
                    setText("False");
        });
        ...
    }
    ...
}