【问题标题】:JavaFX many task work in GUIJavaFX 许多任务在 GUI 中工作
【发布时间】:2019-01-13 05:03:07
【问题描述】:

我运行了一个线程来更新应用程序的打开时间。它运作良好。我已经扩展了服务类。此任务的时间通过 Platform.runLater 更新我的 GUI、textField

@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。我不能运行相同的线程。两个独立的线程可以更新 GUI 吗? 互联网上的大多数信息都专门针对一个主题。谢谢你的每一个建议

【问题讨论】:

    标签: javafx task


    【解决方案1】:

    是的,您可以使用任意数量的线程。您只需确保始终通过 Platform.runLater 进行 GUI 更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2012-10-03
      相关资源
      最近更新 更多