亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

刪除節點時 UI 不更新

刪除節點時 UI 不更新

蕪湖不蕪 2023-03-09 10:47:46
我試圖從我的窗格中逐個刪除所有節點,這樣我就可以看到每一行都被刪除了。為此,我創建了一個新線程并使用了任務類并將方法 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); }
查看完整描述

1 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

您需要使用 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 {

    @Override

    public void start(Stage stage) throws Exception {

        FlowPane pane = new FlowPane();

        pane.setVgap(10);

        pane.setHgap(10);


        Button button1 = new Button("Add Nodes");

        button1.setOnAction(e->{

            Timeline timeline = new Timeline(new KeyFrame(Duration.millis(400), x -> {

                StackPane sp = new StackPane();

                sp.setMinSize(100,100);

                sp.setStyle("-fx-background-color:black,red;-fx-background-insets:0,2;");

                pane.getChildren().add(sp);

            }));

            timeline.setCycleCount(10);

            timeline.play();

        });


        Button button2 = new Button("Remove Nodes");

        button2.setOnAction(e->{

            if(!pane.getChildren().isEmpty()){

                int count = pane.getChildren().size();

                Timeline timeline = new Timeline(new KeyFrame(Duration.millis(400), x -> {

                   if(!pane.getChildren().isEmpty()){ 

                      pane.getChildren().remove(0);

                   }

                }));

                timeline.setCycleCount(count);

                timeline.play();

            }

        });

        VBox root = new VBox(button1, button2,pane);

        root.setSpacing(10);

        Scene sc = new Scene(root, 600, 600);

        stage.setScene(sc);

        stage.show();

    }


    public static void main(String... a) {

        Application.launch(a);

    }

}


查看完整回答
反對 回復 2023-03-09
  • 1 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號