按下按钮后在 TextField 上显示文本 (JavaFX)

Show text on TextField after button is pressed (JavaFX)

我正在尝试构建一个计算器,我在整个互联网上都看过了,但示例对我没有帮助,所以我创建了按钮和所有内容,我试图在以下位置显示:

    TextField Result = new TextField();
    Result.setEditable(false);
    Result.setAlignment(Pos.CENTER_RIGHT);
    Result.setMinSize(210, 30);
    Result.textProperty().bind(Bindings.format("%.0f" , value));    
    pane.getChildren().add(Result);

按下的按钮的编号,我该怎么做?假设按钮是:

Button uno = new Button("1");

uno.setMinSize(40, 40); pane.getChildren().add(uno);

如何使文本字段结果显示编号 1?

非常感谢!

最简单的方法是为每个按钮添加一个侦听器并动态更改您的 TextField:

uno.pressedProperty().addListener((o, old, newValue) -> Result.setText("1"));
dos.pressedProperty().addListener((o, old, newValue) -> Result.setText("2"));

请注意,当用户按下和松开按钮时,您的侦听器将对 newValue = true/false 做出两次反应。如果需要,进行额外检查:

uno.pressedProperty().addListener((o, old, newValue) -> {
    if (newValue) {
        Result.setText("1")
    }
});

更新: 不要忘记删除此行作为错误的解决方案:

Result.textProperty().bind(Bindings.format("%.0f" , value));