簡體   English   中英

為什么 Thread.sleep() 在 JavaFX 中不起作用?

[英]Why Thread.sleep() doesn't work accordingly in JavaFX?

當我使用 JavaFX 時,睡眠功能不會相應地工作。 就像在這段代碼中:

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");
}

當按下按鈕 b1 時,將調用寫入函數。 現在,在 2 秒后在控制台中打印“Value + i”。 但是那個時候標簽 l1 的文本沒有改變,最后它只變成了“LAST TIME”。 這里有什么問題?

閱讀評論中建議的鏈接后,您可能希望從 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();
}

或者更好地使用 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();
}

我會嘗試創建用於時間延遲的新線程。

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 對象的線程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM