如何以0.1步延迟进度条中的setProgress直到满
How to delay setProgress in Progress Bar in 0.1 steps until full
我正在尝试制作一个进度条,它将以 0.1 步为单位设置进度,延迟 1000 毫秒,直到 "full"。
我已经找到了如何延迟一步的解决方案,但无法将其放入 for 循环中,该循环将进度设置为 0.1 步,直到进度等于 1,因此已满。
我需要如何修改下面的解决方案才能实现?
package project;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Progress extends Application {
StackPane stack = new StackPane();
Scene scene = new Scene(stack, 400, 800);
// Progress Bar
ProgressBar progressBar = new ProgressBar();
public void start(Stage primaryStage) throws Exception {
// Progress Bar
stack.getChildren().add(progressBar);
progressBar.setTranslateX(0);
progressBar.setTranslateY(0);
progressBar.setProgress(0);
Task<Void> sleeper = new Task<Void>() {
@Override
protected Void call() throws Exception {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return null;
}
};
sleeper.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
progressBar.setProgress(0.1);
}
});
new Thread(sleeper).start();
primaryStage.setScene(scene);
primaryStage.setTitle("Title");
primaryStage.show();
}
public static void main(String[] args) {
launch();
}
}
让您的任务执行迭代,并在进行时更新它的进度:
Task<Void> sleeper = new Task<Void>() {
@Override
protected Void call() throws Exception {
final int numIterations = 10 ;
for (int i = 0 ; i < numIterations ; i++) {
updateProgress(i, numIterations);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
updateProgress(numIterations, numIterations);
return null;
}
};
那就把进度条的进度绑定到任务的进度上:
progressBar.progressProperty().bind(sleeper.progressProperty());
我正在尝试制作一个进度条,它将以 0.1 步为单位设置进度,延迟 1000 毫秒,直到 "full"。
我已经找到了如何延迟一步的解决方案,但无法将其放入 for 循环中,该循环将进度设置为 0.1 步,直到进度等于 1,因此已满。
我需要如何修改下面的解决方案才能实现?
package project;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Progress extends Application {
StackPane stack = new StackPane();
Scene scene = new Scene(stack, 400, 800);
// Progress Bar
ProgressBar progressBar = new ProgressBar();
public void start(Stage primaryStage) throws Exception {
// Progress Bar
stack.getChildren().add(progressBar);
progressBar.setTranslateX(0);
progressBar.setTranslateY(0);
progressBar.setProgress(0);
Task<Void> sleeper = new Task<Void>() {
@Override
protected Void call() throws Exception {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return null;
}
};
sleeper.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
progressBar.setProgress(0.1);
}
});
new Thread(sleeper).start();
primaryStage.setScene(scene);
primaryStage.setTitle("Title");
primaryStage.show();
}
public static void main(String[] args) {
launch();
}
}
让您的任务执行迭代,并在进行时更新它的进度:
Task<Void> sleeper = new Task<Void>() {
@Override
protected Void call() throws Exception {
final int numIterations = 10 ;
for (int i = 0 ; i < numIterations ; i++) {
updateProgress(i, numIterations);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
updateProgress(numIterations, numIterations);
return null;
}
};
那就把进度条的进度绑定到任务的进度上:
progressBar.progressProperty().bind(sleeper.progressProperty());