删除节点时 UI 不更新

我试图从我的窗格中逐个删除所有节点,这样我就可以看到每一行都被删除了。为此,我创建了一个新线程并使用了任务类并将方法 delWalls() 包装在平台中。运行后()。然后我使用 Thread.sleep 认为它会减慢循环速度所以我可以看到 UI 在每一行被删除时更新但是会发生什么情况是整个 UI 冻结然后在循环完成后所有节点都消失了?有没有办法解决这个......谢谢


*所有节点都是线顺便说一句


 //loop calls delWalls() 1458 times to delete all 1458 nodes sequentailly

    Task task = new Task<Void>() {

        @Override

        public Void call() {

            Platform.runLater(() -> {

                try {

                    for (int i = 0; i <= 1458 - 1; i++) {

                        Thread.sleep(2);



                        delWalls();

                    }

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            });


            return null;

        }

    };

    new Thread(task).start();



    }

//delWalls方法每调用一次删除一个节点。


  public void delWalls() throws InterruptedException {


    pane.getChildren().remove(0);

 }


芜湖不芜
浏览 84回答 1
1回答

回首忆惘然

您需要使用 Timeline 才能获得所需的效果。以下是如何完成的快速示例演示。单击“添加”依次添加节点,添加完所有 10 个节点后,单击“删除”将它们一一删除。import javafx.animation.KeyFrame;import javafx.animation.Timeline;import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.FlowPane;import javafx.scene.layout.StackPane;import javafx.scene.layout.VBox;import javafx.stage.Stage;import javafx.util.Duration;public class RemoveNodes_Demo extends Application {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void start(Stage stage) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; FlowPane pane = new FlowPane();&nbsp; &nbsp; &nbsp; &nbsp; pane.setVgap(10);&nbsp; &nbsp; &nbsp; &nbsp; pane.setHgap(10);&nbsp; &nbsp; &nbsp; &nbsp; Button button1 = new Button("Add Nodes");&nbsp; &nbsp; &nbsp; &nbsp; button1.setOnAction(e->{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Timeline timeline = new Timeline(new KeyFrame(Duration.millis(400), x -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StackPane sp = new StackPane();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sp.setMinSize(100,100);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sp.setStyle("-fx-background-color:black,red;-fx-background-insets:0,2;");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pane.getChildren().add(sp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timeline.setCycleCount(10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timeline.play();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; Button button2 = new Button("Remove Nodes");&nbsp; &nbsp; &nbsp; &nbsp; button2.setOnAction(e->{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!pane.getChildren().isEmpty()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int count = pane.getChildren().size();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Timeline timeline = new Timeline(new KeyFrame(Duration.millis(400), x -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(!pane.getChildren().isEmpty()){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pane.getChildren().remove(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timeline.setCycleCount(count);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timeline.play();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; VBox root = new VBox(button1, button2,pane);&nbsp; &nbsp; &nbsp; &nbsp; root.setSpacing(10);&nbsp; &nbsp; &nbsp; &nbsp; Scene sc = new Scene(root, 600, 600);&nbsp; &nbsp; &nbsp; &nbsp; stage.setScene(sc);&nbsp; &nbsp; &nbsp; &nbsp; stage.show();&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String... a) {&nbsp; &nbsp; &nbsp; &nbsp; Application.launch(a);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java