为什么 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 时,将调用写入函数。现在,在控制台中 "Value + i" 会在 2 秒后打印出来。但是此时Label 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 对象的线程。
当我使用 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 时,将调用写入函数。现在,在控制台中 "Value + i" 会在 2 秒后打印出来。但是此时Label 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 对象的线程。