gpt4 book ai didi

java - 再次完成后在 JavaFX 中运行相同的任务

转载 作者:行者123 更新时间:2023-12-03 12:58:31 25 4
gpt4 key购买 nike

我在尝试什么:

我尝试为家庭锻炼建立一个倒数计时器,它以相同的间隔运行两次,然后给您额外的间隔休息时间。之后,它应该以 3 个间隔重新开始。

我走了多远?:

目前我成功地运行了从三十秒到零的第一个间隔。我的问题是,我无法确定 JavaFX 任务是否已完成。更准确地说,如果不创建几个自覆盖过程(例如使用 for 循环),我就无法重新开始。

代码和图形用户界面:

这是我的 Controller.class用于处理我的 FXML 文件:

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.Slider;


public class Controller {

private int time;

@FXML
private Label counterLabel;

private static StringProperty displayTime = new SimpleStringProperty("30");

@FXML
private Button startstop;

@FXML
private ProgressIndicator progress;

private static DoubleProperty prog = new SimpleDoubleProperty(0.0);

@FXML
private Slider Pausendauer; // pause length

@FXML
private Slider Trainingsdauer; //interval length

@FXML
private Slider Repeats;

public void initialize() {
progress.setProgress(0);
counterLabel.textProperty().bind(displayTime);
progress.progressProperty().bind(prog);
}


public void click(ActionEvent e) throws InterruptedException {
//event for clicking start button

//Task for Counting (Should be repeatable after running through
time = (int)Trainingsdauer.getValue();
Task<String> count = new TaskTimer(time);
displayTime.bind(count.valueProperty());
prog.bind(count.progressProperty());
count.run();
}
}

那是我的任务(计时器)类:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javafx.concurrent.Task;


public class TaskTimer extends Task<String>{

private int time;
double fraction;
double prog;
public static void main(String[] args) {
}

public TaskTimer(int sec) {
this.time = sec;
this.fraction = 1.0 / time; //claculating for closng circle
this.prog = 0.0;
}

protected String call() throws Exception {
// TODO Auto-generated method stub
//runs scheduled without delay
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable(){
public void run() {
time = time - 1; //Decrement of time
prog = prog + fraction; //increase progress
updateProgress(prog, 1);

if(time >= 10) { // Makes 01 from 1 and so on
updateValue(time+"");
}else {
updateValue("0"+time);
}
System.out.println(time+"");


if (time <= 0) {
updateProgress(1, 1);
scheduler.shutdown();
}
}

}, 1, 1, TimeUnit.SECONDS);

return time+"";

}
}

这就是工作中的应用程序(它能够迭代的可悲之处):

GUI设计来描述功能



接下来我可以尝试什么?现在已经尝试了很多东西。我的大多数“解决方案”最终都以自我覆盖双胞胎和/或卡住的 UI 告终。

最佳答案

您可以使用 Task.setOnSucceeded构造和调用新的方法 Task如下所示 mre :

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class RestartProgressIndicator extends Application {

private static int COUNT_DOWN = 10;
@Override
public void start(Stage stage) {

Work work = new Work();
Button button = new Button("Go");
button.setDefaultButton(true);
button.setOnAction(e -> {
work.work(COUNT_DOWN);
button.setDisable(true);
});

Pane root = new BorderPane(null,work.getPane(),null, button,null);
root.setPadding(new Insets(20.));
Scene scene = new Scene(root, Color.WHITE);
stage.setScene(scene);
stage.centerOnScreen();
stage.show();
}

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

class Work{

private TaskTimer count;
private final ProgressIndicator pi;
private final Pane pane;
private int counter = 0;

Work(){
pi = new ProgressIndicator();
pi.setMinWidth(150); pi.setMinHeight(150);
pane = new Pane(pi);
}

void work(int seconds){

count = new TaskTimer(seconds);
pi.progressProperty().bind(count.progressProperty());

count.setOnSucceeded(e->{
pi.progressProperty().unbind();
if(isRestarart()) {
work(seconds);
}
});

Thread th = new Thread(count);
th.setDaemon(true);
th.start();
}

private boolean isRestarart() {
// TODO apply restart logic
return ++counter < 3;
}

Pane getPane() {
return pane;
}
}

class TaskTimer extends Task<String>{

private final int seconds;
double fraction, prog;

public TaskTimer(int seconds) {
this.seconds = seconds;
fraction = 1.0 / seconds;
prog = 0.0;
}

@Override
protected String call() throws Exception {

int time = seconds;
while(true){
time = time - 1; //Decrement of time
prog = prog + fraction; //increase progress
updateProgress(prog, 1);

if(time >= 10) { // Makes 01 from 1 and so on
updateValue(time+"");
}else {
updateValue("0"+time);
}

TimeUnit.SECONDS.sleep(1);

if (time <= 0) {
updateProgress(1, 1);
break;
}
};

return time+"";
}
}

关于java - 再次完成后在 JavaFX 中运行相同的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62367514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com