【问题标题】:JavaFX - Refresh Label [duplicate]JavaFX - 刷新标签[重复]
【发布时间】:2016-05-30 09:02:54
【问题描述】:

你能解释一下如何刷新标签中的值吗?

在初始化时,我将 Label 的文本绑定到 StringProperty。这里没问题。

我有按钮,按下按钮时我想在每个迭代步骤中更新标签值。 但我只能看到最终值。为什么?

@FXML
private Label label;

@FXML
private void handleButtonAction(ActionEvent event) throws InterruptedException {    
    for(int i=0;i<1001;i++){

        try {
            Thread.sleep(1);
        } catch (InterruptedException ie) {
            //Handle exception
        }            
        this.value.setValue(i+"");                 
    }
}    

// Bind 
private StringProperty value = new SimpleStringProperty("0");

@Override
public void initialize(URL url, ResourceBundle rb) {
    // Bind label to value.
    this.label.textProperty().bind(this.value);
}     

【问题讨论】:

    标签: java javafx javafx-2


    【解决方案1】:

    当您调用 Thread.sleep(1); 时,您实际上停止了 JavaFX 应用程序线程(GUI 线程),因此您阻止它更新 GUI。

    您基本上需要的是一个后台Task,它实际上会停止一段时间,然后通过调用Platform.runLater 来更新JavaFX 应用程序线程上的GUI,然后再进入睡眠状态。

    示例:

    public class MyApplication extends Application {
    
        private IntegerProperty value = new SimpleIntegerProperty(0);
    
        @Override
        public void start(Stage primaryStage) {
            try {
                HBox root = new HBox();
                Scene scene = new Scene(root, 400, 400);
                Label label = new Label();
                Button button = new Button("Press Me");
    
                button.setOnAction(event -> {
                    // Background Task
                    Task<Void> task = new Task<Void>() {
                        @Override
                        protected Void call() {
    
                            for (int i = 0; i < 1001; i++) {
                                int intVal = i;
                                try {
                                    Thread.sleep(1);
                                } catch (InterruptedException ignored) {
                                }
                                // Update the GUI on the JavaFX Application Thread
                                Platform.runLater(() -> value.setValue(intVal));
    
                            }
                            return null;
                        }
                    };
    
                    Thread th = new Thread(task);
                    th.setDaemon(true);
                    th.start();
                });
    
                label.textProperty().bind(value.asString());
                root.getChildren().addAll(button, label);
    
                primaryStage.setScene(scene);
                primaryStage.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    只剩下更新按钮回调了。

    【讨论】:

      猜你喜欢
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 2018-10-05
      • 2019-06-19
      • 1970-01-01
      • 2020-06-22
      相关资源
      最近更新 更多