java - 如何监控文本java fx 中的打印持续时间?

标签 java javafx javafx-8

Is there anyway that I can monitor the time duration in javafx?

可以说是这样的

System.out.println("Printing Duration: " + duration++);

最佳答案

这是我通常如何安排任务的时间。此示例跟踪 ProgressBar 完成所需的秒数。关键是当 Task 开始时启动 Timeline,并在 Task 完成时结束 Timeline

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

public class Example extends Application
{

    @Override
    public void start(Stage stage)
    {
        AtomicInteger atomicInteger = new AtomicInteger();
        Label label = new Label("0 seconds");
        Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), (event) -> {
            System.out.println(atomicInteger.incrementAndGet() + " seconds.");
            label.setText(atomicInteger.get() + " seconds");
        }));
        timeline.setCycleCount(Timeline.INDEFINITE);

        Task<Void> task = new Task<Void>()
        {
            @Override
            protected Void call() throws Exception
            {
                Random random = new Random();
                int randomNumber = random.nextInt(100) + 100;
                for (int i = 0; i < randomNumber; i++) {
                    updateProgress(i, randomNumber);
                    try {
                        Thread.sleep(100);
                    }
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                return null;
            }
        };
        task.setOnSucceeded((event) -> {
            timeline.stop();
        });
        ProgressBar progressBar = new ProgressBar(0);
        progressBar.progressProperty().bind(task.progressProperty());

        Button button = new Button("Start");
        button.setOnAction((event) -> {
            button.setDisable(true);
            timeline.play();
            Thread thread = new Thread(task);
            thread.setDaemon(true);
            thread.start();
        });

        VBox root = new VBox(label, progressBar, button);
        Scene scene = new Scene(root, 500, 500);
        stage.setScene(scene);
        stage.show();
    }

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

}

关于java - 如何监控文本java fx 中的打印持续时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54769610/

相关文章:

java - For循环,切换 boolean 值

java - 在 JavaFx 中格式化 TableView 中的最后一行

JavaFX "toolkit not initialized"在一个测试类中而不是另外两个;区别在哪里?

java - Oracle对我们有什么期望?

layout - 在自定义控件中使用 TextFlow 时,TitledPane 未垂直调整大小

JavaFX:在矩形内设置边框以保持宽度和高度

java - ResultSet.updateTime() 导致错误的值

java - 使用接口(interface)中定义的方法来调用克隆方法

java - 最近更新后无法使用按钮打开带有 LOCK_MODE_LOCKED_CLOSED 的抽屉

java - 如何从 JavaFX 中的另一个 Controller 类访问 UI 元素?