JavaFX简单更新标签(线程)

时间:2018-10-11 08:02:59

标签: java multithreading javafx javafx-2

我正试图向一些初学者展示如何在JavaFX应用程序上设置标签以自动更新。基本上,他们希望该值每分钟左右在标签上减少,而无需任何用户交互。

Java并不是我的强项,通过研究我之前遇到的一些问题,我需要处理线程和Runnable()。

我将代码放在一起可以工作,但是我只是想知道是否有更好的方法或者更简单的代码来演示相同的结果。

public class MainTimer2 extends Application {
    private int count = 100;
    private Label response = new Label(Integer.toString(count));

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

    //Update function
    private void decrementCount() {
        count--;
        response.setText(Integer.toString(count));
    }

    @Override
    public void start(Stage myStage) {
        myStage.setTitle("Update Demo");

        //Vertical and horizontal gaps set to 10px
        FlowPane rootNode = new FlowPane(10, 10);

        rootNode.setAlignment(Pos.CENTER);

        Scene myScene = new Scene(rootNode, 200, 100);

        myStage.setScene(myScene);

        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                Runnable updater = new Runnable() {

                    @Override
                    public void run() {
                        decrementCount();
                    }
                };
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        System.out.println("Timer error");
                    }
                    // UI update is run on the Application thread
                    Platform.runLater(updater);
                }
            }
        });
        // don't let thread prevent JVM shutdown
        thread.setDaemon(true);
        thread.start();
        rootNode.getChildren().addAll(response);
        myStage.show();
    }
}

1 个答案:

答案 0 :(得分:3)

使用PauseTransition倒数:

import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class MainTimer2 extends Application {

    private int count = 100;
    private Label response = new Label(Integer.toString(count));

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

    @Override
    public void start(Stage myStage) {
        myStage.setTitle("Update Demo");

        //Vertical and horizontal gaps set to 10px
        FlowPane rootNode = new FlowPane(10, 10);
        rootNode.setAlignment(Pos.CENTER);

        Scene myScene = new Scene(rootNode, 200, 100);
        myStage.setScene(myScene);

        rootNode.getChildren().addAll(response);
        myStage.show();
        update();
    }

    private void update() {

        PauseTransition pause = new PauseTransition(Duration.seconds(1));
        pause.setOnFinished(event ->{
            decrementCount();
            pause.play();
        });
        pause.play();
    }

    //Update function
    private void decrementCount() {
        count = (count > 0) ? count -1 : 100;
        response.setText(Integer.toString(count));
    }
}

或者,您可以使用Timeline

    private void update() {

        KeyFrame keyFrame = new KeyFrame(
                Duration.seconds(1),
                event -> {
                    decrementCount();
                }
        );
        Timeline timeline = new Timeline();
        timeline.setCycleCount(Animation.INDEFINITE);
        //if you want to limit the number of cycles use 
        //timeline.setCycleCount(100);
        timeline.getKeyFrames().add(keyFrame);
        timeline.play();
    }