【问题标题】:How to update my JavaFX GUI elements while program is running如何在程序运行时更新我的​​ JavaFX GUI 元素
【发布时间】:2019-08-31 00:07:52
【问题描述】:

我希望能够在程序执行时在我的图形界面上看到更新,而不是等到按钮上的点击事件结束

public class MainController implements Initializable {
@FXML
private label label;

@Override
public void initialize(URL url, ResourceBundle rb) {
}

private void Event(ActionEvent event) {
    // start is a button
    if (event.getSource() == Start) {
      // calculations
      // updating label
      label.setText(" update me ");
      // other calculations
      // updating label for the second time
      label.setText(" update me ");
    }
  }
}

这是我的代码(javaFXML)的一个简单示例,请注意计算和更新比演示示例更复杂,执行时间太长,这就是为什么我想在执行时预览更新。

【问题讨论】:

标签: java javafx javafx-8 javafx-2


【解决方案1】:

你应该知道和使用线程的概念

你的代码会是这样的:

if (event.getSource() == Start) {

       new Thread(new Runnable() {
             @Override
             public void run() {
                 //do the calculations
                 System.out.println("calculations is finished");
             }
       }).start();

      // updating label
      label.setText(" update me ");

      new Thread(new Runnable() {
             @Override
             public void run() {
                 //do the other calculations
                 System.out.println("calculations is finished");
             }
       }).start();
      // updating label for the second time
      label.setText(" update me ");
    }

但您必须注意计算之间可能出现的同步问题。 我建议你学习这门课程: https://www.youtube.com/playlist?list=PLBB24CFB073F1048E

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 2014-12-20
    • 2016-11-15
    相关资源
    最近更新 更多