如何监视文本java fx中的打印持续时间? [英] How can I monitor the duration of printing in text java fx?
本文介绍了如何监视文本java fx中的打印持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
反正我可以监视javafx中的持续时间吗?
Is there anyway that I can monitor the time duration in javafx?
让我们这样说吧
System.out.println("Printing Duration: " + duration++);
推荐答案
这是我通常为任务计时的方式.本示例跟踪ProgressBar
完成所需的秒数.关键是在Task
开始时启动Timeline
,在Task
完成时结束Timeline
.
Here is how I normally time a task. This example tracks how many seconds it takes for a ProgressBar
to complete. The key is starting the Timeline
when the Task
starts and ending the Timeline
when the Task
finishes.
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 fx中的打印持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文