如何在JavaFx中单击按钮时更改变量
我想在JavaFX中单击一个按钮时更改变量。 但是,当我尝试使用程序中的变量时,它说
从lambda内部引用的局部变量必须是final或者有效的final。
我不能说它是最终的因为我需要改变它所以我可以使用它。 我的代码看起来像这样
Button next = new Button(); next.setText("next"); next.setOnAction((ActionEvent event) -> { currentLine++; });
我该怎么做才能解决这个问题?
您的问题有各种解决方案。 除了ItachiUchiha的post之外,只需将变量声明为类成员,如下所示:
public class Main extends Application { int counter = 0; @Override public void start(Stage primaryStage) { try { HBox root = new HBox(); Button button = new Button ("Increase"); button.setOnAction(e -> { counter++; System.out.println("counter: " + counter); }); root.getChildren().add( button); Scene scene = new Scene(root,400,400); primaryStage.setScene(scene); primaryStage.show(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); } }
概念
在annonymous inner classes
使用的所有局部变量应该是final or effectively final
即状态在定义后不能改变。
原因
内部类不能引用non final
局部变量的原因是因为本地类实例即使在方法返回后也可以保留在内存中,并且可以更改用于导致synchronization
问题的变量的值。
你怎么克服这个?
1 – 声明一个为您完成工作并在动作处理程序中调用它的方法。
public void incrementCurrentLine() { currentLine++; }
后来称之为:
next.setOnAction((ActionEvent event) -> { incrementCurrentLine(); });
2 – 将currentLine
声明为AtomicInteger 。 然后使用其incrementAndGet()
来递增值。
AtomicInteger currentLine = new AtomicInteger(0);
之后,您可以使用:
next.setOnAction((ActionEvent event) -> { currentLine.incrementAndGet(); // will return the incremented value });
3 – 您还可以声明一个自定义类,在其中声明方法并使用它们。