我有以下的JavaFX应用程序.我希望每次我单击该按钮时,都会增加count
个,并且该按钮被替换为一个显示新计数的新实例.但是,"上一步"按钮仍然可见.为什么打pane.getChildren().removeAll()
好像不管用?
public class App extends Application {
public static void main(String[] args) {
launch();
}
private int count;
public void start(Stage stage) {
count = 0;
var root = new VBox();
var button = createButton(root);
root.getChildren().add(button);
var scene = new Scene(root, 640, 480);
stage.setScene(scene);
stage.show();
}
private void onButtonClicked(final Pane root) {
count += 1;
root.getChildren().removeAll(); // <-- This doesn't work?
root.getChildren().add(createButton(root));
}
private Button createButton(final Pane root) {
var button = new Button("Clicked " + count + " times");
button.setOnAction((_event) -> onButtonClicked(root));
}
}