【问题标题】:JavaFX Simple Update Label (Threading)JavaFX 简单更新标签(线程)
【发布时间】:2018-10-11 08:02:58
【问题描述】:

我正在尝试向一些初学者程序员演示如何在 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();
    }
}

【问题讨论】:

  • 确实有很多方法可以做到这一点。执行此操作的主要方法是使用TimeLineTask。您还可以通过绑定而不是显式调用setText() 来更新标签。总之,我认为您的方法已经很简单并且适合初学者 - 使用其他方法将需要您和您的“观众”更熟悉 JavaFX。但是,请注意,除了控件来自 JavaFX 之外,您的方法并没有使用很多 JavaFX API。
  • 我尝试遵循的规则是,当您要更新 GUI 元素时,请使用 Animation Class 中的内容。不使用线程/定时器时。

标签: java multithreading javafx javafx-2


【解决方案1】:

使用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();
    }

【讨论】:

  • 实际上任何扩展Animation 的东西都可以类似地工作,只有Timeline 具有内置的自动循环功能。
猜你喜欢
  • 2016-01-02
  • 2018-04-19
  • 2020-06-22
  • 1970-01-01
  • 2015-07-08
  • 2015-08-03
  • 2019-12-06
  • 2016-05-10
  • 2017-05-05
相关资源
最近更新 更多