JavaFX - 刷新标签
JavaFX - Refresh Label
你能解释一下如何刷新 Label 中的值吗?
在初始化时,我将 Label 的文本绑定到 StringProperty。这里就可以了
我有 Button,在按下按钮时我想在每个迭代步骤中更新 Label 值。
但我只能看到最终价值。为什么?
@FXML
private Label label;
@FXML
private void handleButtonAction(ActionEvent event) throws InterruptedException {
for(int i=0;i<1001;i++){
try {
Thread.sleep(1);
} catch (InterruptedException ie) {
//Handle exception
}
this.value.setValue(i+"");
}
}
// Bind
private StringProperty value = new SimpleStringProperty("0");
@Override
public void initialize(URL url, ResourceBundle rb) {
// Bind label to value.
this.label.textProperty().bind(this.value);
}
当您调用 Thread.sleep(1);
时,您实际上停止了 JavaFX 应用程序线程(GUI 线程),因此您阻止它更新 GUI。
您基本上需要的是背景 Task
which actually stops for a certain amount of time, then updates the GUI on the JavaFX Application Thread by calling Platform.runLater
再次进入休眠状态。
示例:
public class MyApplication extends Application {
private IntegerProperty value = new SimpleIntegerProperty(0);
@Override
public void start(Stage primaryStage) {
try {
HBox root = new HBox();
Scene scene = new Scene(root, 400, 400);
Label label = new Label();
Button button = new Button("Press Me");
button.setOnAction(event -> {
// Background Task
Task<Void> task = new Task<Void>() {
@Override
protected Void call() {
for (int i = 0; i < 1001; i++) {
int intVal = i;
try {
Thread.sleep(1);
} catch (InterruptedException ignored) {
}
// Update the GUI on the JavaFX Application Thread
Platform.runLater(() -> value.setValue(intVal));
}
return null;
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
});
label.textProperty().bind(value.asString());
root.getChildren().addAll(button, label);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
只剩下更新按钮回调了。
你能解释一下如何刷新 Label 中的值吗?
在初始化时,我将 Label 的文本绑定到 StringProperty。这里就可以了
我有 Button,在按下按钮时我想在每个迭代步骤中更新 Label 值。 但我只能看到最终价值。为什么?
@FXML
private Label label;
@FXML
private void handleButtonAction(ActionEvent event) throws InterruptedException {
for(int i=0;i<1001;i++){
try {
Thread.sleep(1);
} catch (InterruptedException ie) {
//Handle exception
}
this.value.setValue(i+"");
}
}
// Bind
private StringProperty value = new SimpleStringProperty("0");
@Override
public void initialize(URL url, ResourceBundle rb) {
// Bind label to value.
this.label.textProperty().bind(this.value);
}
当您调用 Thread.sleep(1);
时,您实际上停止了 JavaFX 应用程序线程(GUI 线程),因此您阻止它更新 GUI。
您基本上需要的是背景 Task
which actually stops for a certain amount of time, then updates the GUI on the JavaFX Application Thread by calling Platform.runLater
再次进入休眠状态。
示例:
public class MyApplication extends Application {
private IntegerProperty value = new SimpleIntegerProperty(0);
@Override
public void start(Stage primaryStage) {
try {
HBox root = new HBox();
Scene scene = new Scene(root, 400, 400);
Label label = new Label();
Button button = new Button("Press Me");
button.setOnAction(event -> {
// Background Task
Task<Void> task = new Task<Void>() {
@Override
protected Void call() {
for (int i = 0; i < 1001; i++) {
int intVal = i;
try {
Thread.sleep(1);
} catch (InterruptedException ignored) {
}
// Update the GUI on the JavaFX Application Thread
Platform.runLater(() -> value.setValue(intVal));
}
return null;
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
});
label.textProperty().bind(value.asString());
root.getChildren().addAll(button, label);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
只剩下更新按钮回调了。