为什么Thread.sleep()不';在JavaFX中不能相应地工作吗?

为什么Thread.sleep()不';在JavaFX中不能相应地工作吗?,java,javafx,sleep,thread-sleep,Java,Javafx,Sleep,Thread Sleep,当我使用JavaFX时,sleep函数不会相应地工作。与此代码类似: public class Controller { @FXML private Label label; @FXML private Button b1; public void write() throws InterruptedException { label.setText("FIRST TIME"); for(int i=1;i<=5;i++) { System.ou

当我使用JavaFX时,sleep函数不会相应地工作。与此代码类似:

public class Controller {

@FXML private Label label;
@FXML private Button b1;

public void write() throws InterruptedException
{
    label.setText("FIRST TIME");
    for(int i=1;i<=5;i++)
    {
        System.out.println("Value "+i);
        label.setText("Value "+i);
        Thread.sleep(2000);
    }
    label.setText("LAST TIME");
}
公共类控制器{
@FXML专用标签;
@FXML专用按钮b1;
public void write()引发InterruptedException
{
label.setText(“第一次”);

对于(int i=1;i在阅读了评论中建议的链接后,您可能希望从fx线程中删除长进程(延迟)。
您可以通过调用另一个线程来执行此操作:

public void write() {

    label.setText("FIRST TIME");

    new Thread(()->{ //use another thread so long process does not block gui
        for(int i=1;i<=6;i++)   {
            String text;
            if(i == 6 ){
                text = "LAST TIME";
            }else{
                 final int j = i;
                 text = "Value "+j;
            }

            //update gui using fx thread
            Platform.runLater(() -> label.setText(text));
            try {Thread.sleep(2000);} catch (InterruptedException ex) { ex.printStackTrace();}
        }

    }).start();
}
public void write(){
label.setText(“第一次”);
新线程(()->{//请使用另一个线程,这样长的进程不会阻塞gui
对于(inti=1;i label.setText(text));
尝试{Thread.sleep(2000);}catch(InterruptedException ex){ex.printStackTrace();}
}
}).start();
}
或者更好地使用fx动画工具,如:

private int i = 0; // a filed used for counting 

public void write() {

    label.setText("FIRST TIME");

    PauseTransition pause = new PauseTransition(Duration.seconds(2));
    pause.setOnFinished(event ->{
        label.setText("Value "+i++);
        if (i<=6) {
            pause.play();
        } else {
            label.setText("LAST TIME");
        }
    });
    pause.play();
}
private int i=0;//用于计数的字段
公共空写(){
label.setText(“第一次”);
暂停转换暂停=新的暂停转换(持续时间为秒(2));
暂停。设置完成(事件->{
label.setText(“值”+i++);

如果(i我将尝试创建用于延时的新线程

package sample;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

public class Controller implements Runnable{


    private volatile boolean isRunning = false;
    private Thread timer;

    private int delay = 2000;

    @FXML
    private Label label;

    @FXML
    private Button button;


    private void start(){
        timer = new Thread(this, "timer");
        isRunning = true;
        timer.start();
    }

    private void stop(){
        isRunning = false;
    }

    private void interrupt(){
        isRunning = false;
        timer.interrupt();
    }

    @Override
    public void run() {
        int counter = 0;
        while (isRunning) {
            try {
                ++counter;
                String text = "MyText" + counter;
                Platform.runLater(() -> label.setText(text));

                if (counter == 5) {
                    stop();
                }


                Thread.currentThread().sleep(delay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

必须更新
Platform.runLater()下的标签
-JavaFx主线程是唯一允许更新JavaFx对象的线程。

因为JavaFx与大多数GUI框架一样,是单线程的,这意味着任何阻止其事件线程的操作都将阻止其更新UI,直到它们被解除阻止为止(即执行离开该方法)-在你打开一个新线程之前,它也不是线程安全的,你应该通过阅读来更好地理解它。JavaFX是基于动画的框架(动画是它设计的关键部分)它有许多有用的API来帮助简化它-也许你应该看看类似的东西,它可能与你的问题重复。我建议创建一个新线程作为计时器,并通过调用
Platform.runLater(Runnable Runnable)将标签对象的更新委托给JavaFx主线程
您可以通过{isRunnig&&counter>时的
来简化一点