当前位置: 首页 > 知识库问答 >
问题:

移除节点时UI不更新

葛鸿熙
2023-03-14

我正在尝试按顺序逐个删除窗格中的所有节点,以便可以看到每一行被删除。为此,我创建了一个新线程,使用task类,并将方法delWalls()包装在一个平台中。runLater()。然后我用了线。睡眠会让循环变慢,这样当每一行被删除时,我就可以看到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个答案

米夕
2023-03-14

正如@MadProgrammer所说,你需要使用时间线来获得想要的效果。下面是一个快速的示例演示,演示了如何做到这一点。单击“添加”按顺序添加节点,添加完所有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);
    }
}
 类似资料:
  • 有些情况下,需要在移除节点之前关闭节点。如果节点变成不可达,或者没有响应。我们可以像如下方法使用--force参数强行移除节点。 $ docker node rm node9 Error response from daemon: rpc error: code = 9 desc = node node9 is not down and can't be removed $ docker node

  • 我正在玩一个链接列表类项目的指针,我不知道如何创建到新节点的链接。我有一个类,它包含像这样的方法来操作数据结构。我希望这些节点是从csv文件中读取的出价。 当我从CSV加载所有数据时,我想 创建一个新的出价 将新的出价传递给函数 设置Bid对象的nextBid指针,并更新链接列表的尾部 我将不胜感激为每个出价对象创建新地址的任何指针,因为现在尾节点只'记得'第一个出价的地址。 我复制了下面的代码,

  • 我在馆长2.12.0中使用动物园管理员。 通过使用watcher调用getChildren(我事先不知道完整的节点路径),我成功地监视了新节点,watcher再次提交任务以调用getChildren with watcher。现在我想看节点移除和仅移除。我打电话给守望者。但如果节点由于某种原因不存在,它实际上将是节点创建的观察者,在我的例子中,这是永远不会发生的。因此,我将留下越来越多的“鞭打”观

  • 我很难找出平均和最坏情况下的时间复杂度。因此,我使用以下逻辑删除了BST节点 删除二元搜索树中的节点时,有3种情况 那么,如何计算总体平均时间复杂度和最差时间复杂度??

  • 当我通过GPS移除一个节点时,当我试图打印二叉树时,会出现堆栈溢出。下面是我的一些代码。我不能理解它将如何工作良好,如果我删除的名称,但不是,如果我删除的坐标,因为我正在使用相同的删除方法。 我得到的确切错误是“Exe:0xC00000FD中0x013214D6处的未处理异常:堆栈溢出(参数:0x00000001,0x00152FFC)”。在按坐标删除后,在打印函数中会出现这种情况,但如果按名称删