java - 任务调度程序,共享表单标签变量

标签 java javafx time task

我正在用 Java FX 编写一些简单的程序。我是 Java 初学者。 我想做, 您填写文本框(时间以分钟为单位)并运行它。 然后运行timer.schedule,它会每分钟刷新标签一次。像秒表之类的东西。 (您设置您想要记住的时间)。

我有 Controller

public class Controller implements Initializable  {

    @FXML
    public Label label;

    @FXML
   private TextField timeEnd;
    .
    .

以及onClick方法

@FXML
private void handleButtonAction(ActionEvent event) {
    Integer timeEndVal = Integer.parseInt(timeEnd.getText());
    Date startDate = new Date();
    Date endDate = new Date();
    endDate.setTime(startDate.getTime() + (timeEndVal * 60000));

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            long currentMinutes = currentTime().getTime() - startDate.getTime();

            System.out.println(currentMinutes / 60000 + " / " + timeEndVal + " min");
            label.setText(String.valueOf(currentMinutes / 60000 + " / " + timeEndVal + " min"));


        }
    }, 0, 60000);

但我不知道如何将 label 变量获取到 timer.schedule。 我做错了什么。感谢您的帮助。

最佳答案

这是一个非 Controller 版本,您可以从中获取一些想法。我建议您使用 TimeLine 而不是 Timer。这不是一个完整的程序!

import java.util.concurrent.atomic.AtomicInteger;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication267 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        AtomicInteger timeInSeconds = new AtomicInteger();
        TextField textField = new TextField();
        Label label = new Label(Integer.toString(timeInSeconds.get()));
        Button btn = new Button("Play");

        textField.setPromptText("Enter the number of minutes");
        textField.textProperty().addListener((obs, oldValue, newValue) -> {
            //This assume the input is corret!
            timeInSeconds.set(Integer.parseInt(newValue) * 60);//Number of minutes times 60 seconds
            label.setText(getMinsSeconds(timeInSeconds.get()));
        });

        Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), (event) -> {
            if (timeInSeconds.get() > 0) {
                label.setText(getMinsSeconds(timeInSeconds.decrementAndGet()));
            }
        }));
        timeline.setCycleCount(Timeline.INDEFINITE);

        btn.setOnAction((ActionEvent event) -> {
            switch (btn.getText()) {
                case "Play":
                    timeline.play();
                    btn.setText("Pause");
                    textField.setEditable(false);
                    break;
                case "Pause":
                    timeline.pause();
                    btn.setText("Play");
                    textField.setEditable(true);
                    break;
            }
        });

        VBox root = new VBox(label, btn, textField);

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

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

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

    String getMinsSeconds(int seconds)
    {
        return String.format("%02d:%02d", (seconds / 60), (seconds % 60));
    }
}

关于java - 任务调度程序,共享表单标签变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52448890/

相关文章:

java - 如何遍历 JavaFX 上的按钮列表并根据所述按钮的数据显示场景

java - TableColumn::setOnEditCommit() 中的 Lambda 运算符

java - 如何将Javafx(非fxml)分离为fxml和 Controller ? (调用目标异常)

performance - 如何将准确度绘制为 Keras 中处理时间的函数?

java - 时间比较

java - 为什么分配方法返回值会使应用程序运行得比仅仅调用它更快?

java - 使用 war 时组到 IBM Liberty (WLP) 中的角色映射

java - 这是在后台线程中运行代码并通过在调用线程中执行的回调返回结果的便捷方式吗?

javafx - 使用 slider 更改文本颜色

c++ - 从午夜起,我很难从一年中的几天和几秒钟获取日期/时间部分