代码之家  ›  专栏  ›  技术社区  ›  Light

JavaFX许多任务在GUI中工作

  •  0
  • Light  · 技术社区  · 6 年前

    我运行了一个更新应用程序打开时间的线程。它工作得很好。我已经扩大了服务范围。此任务的时间将更新我的GUI textField by Platform.runLater

    @Override
    protected Task<Void> createTask() {
        return new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                while (!isCancelled()) {
                    if (isPause == false) {
    
                        try {
                            Platform.runLater(() -> {
                                   currentTimeInApp = currentTimeInApp + 1;
                                   upPanelController.timeInApp.setText
                                        (currentTimeInApp.toString());
                                }
                            });
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            System.out.println(e.getMessage());
                        }
    
                        if (isCancelled())
                            break;
                    }
                }
                return null;
            }
        };
    }
    

    我想运行第二个线程,也更新图形用户界面。我不能运行相同的线程。两个独立的线程可以更新GUI吗? 互联网上的大部分信息都集中在一个主题上。谢谢你的每一个建议

    1 回复  |  直到 6 年前
        1
  •  2
  •   mipa    6 年前

    是的,你想用多少就用多少。您只需确保始终通过Platform.runLater进行GUI更新。