javafx-label不更新任务进度
下面是javafx中关于javafx-label不更新任务进度,java,javafx,javafx-2,Java,Javafx,Javafx 2,下面是javafx中关于任务s的一个简单示例,其中标签文本根据任务的进度进行更新 由于某种原因,标签不显示进度,而是从-1跳到1(最终值) 谢谢你的帮助 public class MultithreadingMain extends Application { @Override public void start(Stage primaryStage) throws Exception { HBox root = new HBox();
任务
s的一个简单示例,其中标签
文本根据任务的进度进行更新
由于某种原因,标签
不显示进度,而是从-1跳到1(最终值)
谢谢你的帮助
public class MultithreadingMain extends Application {
@Override
public void start(Stage primaryStage) throws Exception
{
HBox root = new HBox();
Button button = new Button("Go");
Task<Integer> task = new Task<Integer>() {
@Override
public Integer call() throws InterruptedException
{
int max = 100;
for(int i=0; i<max; i++)
{
Thread.sleep(50);
updateProgress(i+1,max);
}
System.out.println("Done");
return 0;
}
};
button.setOnAction(e -> {
Thread t = new Thread(task);
t.run();
});
Label label = new Label("");
label.textProperty().bind(task.progressProperty().asString());
root.getChildren().add(button);
root.getChildren().add(label);
Scene scene = new Scene(root, 400,400);
primaryStage.setTitle("Multithreading in java fx");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
public类多线程main扩展应用程序{
@凌驾
public void start(Stage primaryStage)引发异常
{
HBox根=新的HBox();
按钮按钮=新按钮(“Go”);
任务=新任务(){
@凌驾
公共整数调用()引发InterruptedException
{
int max=100;
对于(int i=0;i{
线程t=新线程(任务);
t、 run();
});
标签=新标签(“”);
label.textProperty().bind(task.progressProperty().asString());
root.getChildren().add(按钮);
root.getChildren().add(标签);
场景=新场景(根,400400);
setTitle(“JavaFX中的多线程”);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args)
{
发射(args);
}
}
您没有正确使用,只需执行代码而不创建新线程。将t.run();
更改为t.start();