【问题标题】:setTitle() with Thread.sleep()setTitle() 与 Thread.sleep()
【发布时间】:2018-09-10 00:34:31
【问题描述】:

我刚刚在javafx中了解到一些消息,然后我想应用它们,所以我选择做一个计数器;但是当我在标签上设置文本时不会改变它;我用 System.out.print 对其进行了测试,效果很好。 这是我的代码:

public class Controller {
@FXML
private Label time;
@FXML
private Button start;
public void Counter(ActionEvent event)
{
    start.setText("hello");
    time.setText("00 : 00 : 00");
    int h=0,m=0,s=0,i=0;
    while(true)
    {
        try {
            s++;
            if(s==60) {s=0;m++;}
            if(m==60) {m=0;h++;}
            System.out.println(Integer.toString(h)+" : "+String.valueOf(m)+" : "+String.valueOf(s));
            time.setText(Integer.toString(h)+" : "+String.valueOf(m)+" : "+String.valueOf(s));

                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



    }

}
}

感谢您的帮助! ;)

【问题讨论】:

标签: java eclipse multithreading user-interface javafx


【解决方案1】:

这里重要的是,您需要了解对 UI 进行的任何更新都需要在 UI 的主线程中完成。您还应该避免阻塞 UI 的主线程,这将阻止 UI 更新。

首先,请阅读Concurrency in JavaFX

当您处理时间或持续时间时,您应该始终使用专用库,例如​​ java.time。为什么?因为实际上一天没有 24 小时,也没有每分钟 60 秒 - 我知道,这很奇怪。

你可以...

使用Timeline

import java.time.LocalDateTime;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Test extends Application {

    private LocalDateTime startTime;
    private Timeline timer;

    private Label time;

    @Override
    public void start(Stage primaryStage) {
        time = new Label("...");
        Button btn = new Button();
        btn.setText("Start");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                if (timer != null) {
                    timer.stop();
                }
                startTime = LocalDateTime.now();
                timer = new Timeline(new KeyFrame(Duration.millis(500), new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent event) {
                        LocalDateTime now = LocalDateTime.now();
                        java.time.Duration duration = java.time.Duration.between(startTime, now);
                        time.setText(format(duration));
                    }
                }));
                timer.setCycleCount(Timeline.INDEFINITE);
                timer.play();
            }
        });

        VBox root = new VBox();
        root.setAlignment(Pos.CENTER);
        root.getChildren().add(time);
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    protected String format(java.time.Duration duration) {
        long hours = duration.toHours();
        long mins = duration.minusHours(hours).toMinutes();
        long seconds = duration.minusMinutes(mins).toMillis() / 1000;
        return String.format("%02dh %02dm %02ds", hours, mins, seconds);
    }

    public static void main(String[] args) {
        launch(args);
    }

}

使用AnimationTimer

这可能是首选的解决方案,因为定时器的调用与动画引擎的帧刷新同步

import java.time.LocalDateTime;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Test extends Application {

    private LocalDateTime startTime;
    private AnimationTimer timer;

    private Label time;

    @Override
    public void start(Stage primaryStage) {
        time = new Label("...");
        Button btn = new Button();
        btn.setText("Start");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                if (timer != null) {
                    timer.stop();
                }
                startTime = LocalDateTime.now();
                timer = new AnimationTimer() {
                    @Override
                    public void handle(long click) {
                        LocalDateTime now = LocalDateTime.now();
                        java.time.Duration duration = java.time.Duration.between(startTime, now);
                        time.setText(format(duration));
                    }
                };
                timer.start();
            }
        });

        VBox root = new VBox();
        root.setAlignment(Pos.CENTER);
        root.getChildren().add(time);
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    protected String format(java.time.Duration duration) {
        long hours = duration.toHours();
        long mins = duration.minusHours(hours).toMinutes();
        long seconds = duration.minusMinutes(mins).toMillis() / 1000;
        return String.format("%02dh %02dm %02ds", hours, mins, seconds);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

【讨论】:

    【解决方案2】:

    永远不应该使用 while(true) 循环来更新 JavaFX GUI,您可以改用 AnimationTimer

    你可以试试这个

    public void Counter(ActionEvent event)
    {
            start.setText("hello");
            time.setText("00 : 00 : 00");
    
            AnimationTimer timer = new AnimationTimer() {
                long then=0;
                double sum=0;
                int h=0,m=0,s=0;
    
                @Override
                public void handle(long now) {
                    long dt = now - then;
                    sum+=dt;
                    if(sum/1000000000>=1) {
                         s++;
                            if(s==60) {s=0;m++;}
                            if(m==60) {m=0;h++;}
                            System.out.println(Integer.toString(h)+" : "+String.valueOf(m)+" : "+String.valueOf(s));
                            time.setText(Integer.toString(h)+" : "+String.valueOf(m)+" : "+String.valueOf(s));
                        sum=0;
                    }
    
                    then=now;
                }
    
            };
            timer.start();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      • 2013-04-28
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多