JavaFX,变量不会在 actionEvent 上更新

JavaFX, variable does not update on actionEvent

我有一个问题,我有一个按钮,单击时我想更新一个值,但我无法更新它,因为 lambda 表达式需要是最终的。在此示例中,我想将更改编号添加到当前编号并保存,因此有一个新的当前编号,每次单击按钮时,它都会接受并添加更改。我怎样才能解决这个问题?

This is the class, I want when one of the buttons with an arrow is selected to either increase or decrease the value by 'int change'.现在,当您单击该按钮时,它会获取当前值并将其增加 'change' 但它不会更新它?我该如何解决这个问题?

还有,当我尝试了所有方法但都不起作用时,如何向 imageView 添加填充或边距?或者我可以向 borderPane 的特定部分添加填充吗?

Image with buttons

private Pane getControls(String paneName, String imageName, String symbol, double min, double max, double def, double change) {

        BorderPane borderPane = new BorderPane();

        ImageView image = getImage(imageName);
        image.setFitHeight(80);
        image.setFitWidth(60);

        Label defLabel;

        int current = (int) def;

        if (min > 20) {
            int def1 = (int) def;
            defLabel = new Label(def1 + "");
        } else {
            defLabel = new Label(def + "");
        }

        defLabel.setStyle("-fx-text-fill: #ff0000;");
        defLabel.setFont(new Font(50));

        Label symbolLabel = new Label(symbol);
        symbolLabel.setStyle("-fx-text-fill: #ffffff;");
        symbolLabel.setFont(new Font(24));

        symbolLabel.setPadding(new Insets(30, 40, 0, 0));

        borderPane.setRight(symbolLabel);
        borderPane.setCenter(defLabel);
        borderPane.setLeft(image);

        ImageView imageUp = getImage("up-icon.png");
        ImageView imageDown = getImage("down-icon.png");

        imageUp.setFitHeight(50);
        imageUp.setFitWidth(50);

        imageDown.setFitHeight(50);
        imageDown.setFitWidth(50);

        Button buttonUp = new Button();
        Button buttonDown = new Button();

        buttonUp.setOnAction((ActionEvent event) -> {
            defLabel.setText((current + (int) change) + "");
            current += (int) change;
            System.out.println((current + (int) change) + " Up button pressed.");
        });
        buttonUp.setGraphic(imageUp);
        buttonDown.setGraphic(imageDown);

        buttonUp.setPrefSize(50, 50);
        buttonDown.setPrefSize(50, 50);
        buttonUp.setStyle("-fx-background-color: #6b4218;");
        buttonDown.setStyle("-fx-background-color: #6b4218;");

        HBox hbox = new HBox(12);

        hbox.getChildren().add(buttonUp);
        hbox.getChildren().add(buttonDown);
        hbox.setAlignment(Pos.CENTER);
        hbox.setPadding(new Insets(0, 0, 24, 0));

        borderPane.setBottom(hbox);
        borderPane.setPadding(new Insets(100));

        BorderedTitledPane borderedTitledPane = new BorderedTitledPane(paneName, borderPane, 300, 200);
        return borderedTitledPane;
    }

我会尝试用一个例子向你解释

如果你不明白我的解释,那么请阅读这个教程(那里解释得很好):https://www.baeldung.com/java-lambda-effectively-final-local-variables

如果可以将变量设置为 final,则只能从 lambda 中访问该变量:

int i = 5; // You could set this variable to final and it would still compile
    
runSomething(() -> {
    System.out.println(i); // Will compile since i could be set final
});

这就是当您再次更改 i 时发生的情况:

int i = 5; // i was defined
// ...
i = 10; // assigned new value to i therefore i could no longer be final

runSomething(() -> {
    System.out.println(i); // Will NOT compile since i could not be final anymore after it got changed
});

在 Java 1.8+ 之前,我们必须像这样在实现接口(/ lambda(自 1.8 起才存在))时声明变量 final 才能使用它们。


因此,要从外部访问变量(除非它是 class 的字段)在 lamda 内部,您需要能够将其设为最终变量。您永远无法更改这样的值(就像您尝试的那样),因为您无法更改最终变量。你的 'current' 变量在你的 lambda 内部 'final',即使你从未将它设置为 final(这是因为包含该变量的方法可能在从其他地方调用 lamda 之前已经完成执行. 因为你不能改变过去的变量在 lamda 体内是不可改变的)。


顺便说一句。你的 'defLabel' 也在你的 lambda 体内 'final',因为它从未改变(这也是你可以在 lamda 内部访问它的原因)