User interface Javafx中的ProgressBar在onAction块中不更新
我希望有一个在onAction块中更新的进度条。由于以下代码中的某些原因,进度条在退出onAction块之前不会更新。我在9秒钟内没有进步,然后是90%。系统输出打印得很好User interface Javafx中的ProgressBar在onAction块中不更新,user-interface,javafx,progress,User Interface,Javafx,Progress,我希望有一个在onAction块中更新的进度条。由于以下代码中的某些原因,进度条在退出onAction块之前不会更新。我在9秒钟内没有进步,然后是90%。系统输出打印得很好 package net.snortum.play; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets;
package net.snortum.play;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class ProgressBarTester extends Application {
public static void main( String[] args ) {
launch( ProgressBarTester.class, args );
}
@Override
public void start( final Stage stage ) throws Exception {
final GridPane grid = new GridPane();
grid.setAlignment( Pos.CENTER );
grid.setHgap( 10 );
grid.setVgap( 10 );
grid.setPadding( new Insets( 25, 25, 25, 25 ) );
final Text title = new Text( "Test Progress Bar" );
int col = 0, row = 0, colSpan = 2, rowSpan = 1;
grid.add( title, col, row, colSpan, rowSpan );
col = 0;
row++;
grid.add( new Label( "Progress:" ), col, row );
col = 1;
final ProgressBar progress = new ProgressBar( 0.0 );
grid.add( progress, col, row );
final HBox hbox = new HBox();
hbox.setAlignment( Pos.BASELINE_RIGHT );
final Button submit = new Button( "Go" );
hbox.getChildren().add( submit );
col = 1;
row++;
grid.add( hbox, col, row );
submit.setOnAction( new EventHandler<ActionEvent>() {
@Override
public void handle( ActionEvent event ) {
double size = 10.0;
for (double i = 0.0; i < size; i++){
progress.setProgress( i / size );
System.out.printf("Complete: %02.2f%n", i * 10);
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
} );
final Scene scene = new Scene( grid );
stage.setScene( scene );
stage.setTitle( "Test Progress Bar" );
stage.show();
}
}
您正在阻止javafx事件线程,您需要在另一个线程中执行工作,并仅将setProgress调用与平台同步。runLater您正在阻止javafx事件线程,您需要在另一个线程中执行工作,并仅将setProgress调用与平台同步。runLater是tomsomtom精确的答复: 如果您使用JavaFX线程来做这种长期的工作,您将停止UI的更新等,因此您必须在sparte线程中运行worker。如果在单独的线程中运行,则必须使用runLater将所有GUI操作(如progress.setProgress)传递回JavaFX线程,因为JavaFX不是多线程的 在你的行动中尝试以下方法:
submit.setOnAction( new EventHandler<ActionEvent>() {
@Override
public void handle( ActionEvent event ) {
double size = 10.0;
new Thread(){
public void run() {
for (double i = 0.0; i < size; i++){
final double step = i;
Platform.runLater(() -> progress.setProgress( step / size ));
System.out.printf("Complete: %02.2f%n", i * 10);
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}.start();
}
} );
tomsomtom精确回答的详细形式: 如果您使用JavaFX线程来做这种长期的工作,您将停止UI的更新等,因此您必须在sparte线程中运行worker。如果在单独的线程中运行,则必须使用runLater将所有GUI操作(如progress.setProgress)传递回JavaFX线程,因为JavaFX不是多线程的 在你的行动中尝试以下方法:
submit.setOnAction( new EventHandler<ActionEvent>() {
@Override
public void handle( ActionEvent event ) {
double size = 10.0;
new Thread(){
public void run() {
for (double i = 0.0; i < size; i++){
final double step = i;
Platform.runLater(() -> progress.setProgress( step / size ));
System.out.printf("Complete: %02.2f%n", i * 10);
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}.start();
}
} );