Multithreading 在JavaFX中使用Thread.sleep获得等待效果
我想实现如下功能:用户按下登录按钮,然后标签显示: “连接。”Multithreading 在JavaFX中使用Thread.sleep获得等待效果,multithreading,javafx,Multithreading,Javafx,我想实现如下功能:用户按下登录按钮,然后标签显示: “连接。” 0.5秒时间间隔 “连接…” 0.5秒时间间隔 “连接…” 等 只是一种视觉效果,表明“引擎盖下”确实发生了什么 我所得到的并不完全是我所期望的。我点击按钮,等待1.5秒,然后我得到了“连接…”,错过了前面的两个步骤 首先,我的状态类 public class Status { private static StringProperty status = new SimpleStringProperty(); pu
0.5秒时间间隔
“连接…”
0.5秒时间间隔
“连接…”
等 只是一种视觉效果,表明“引擎盖下”确实发生了什么 我所得到的并不完全是我所期望的。我点击按钮,等待1.5秒,然后我得到了“连接…”,错过了前面的两个步骤 首先,我的
状态
类
public class Status {
private static StringProperty status = new SimpleStringProperty();
public static void setStatus(String newStatus) {
status.setValue(newStatus);
}
public static String getStatus() {
return status.getValue();
}
public static StringProperty get() {
return status;
}
}
和我的LoginView
课程
public class LoginView extends Application {
private Button loginButton = new Button("Log in");
private Label statusLabel;
private void createLabels() {
statusLabel = new Label(Status.getStatus());
statusLabel.textProperty().bind(Status.get());
}
}
private void createButtons() {
loginButton.setOnAction(e -> {
try {
Status.setStatus("Connecting.");
Thread.sleep(500);
Status.setStatus("Connecting..");
Thread.sleep(500);
Status.setStatus("Connecting...");
Thread.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
});
}
您应该使用
Timeline
API制作动画。请看这里:
基本上,您只需定义距离为0.5秒的关键帧
s,然后设置文本值以添加另一个点。还可以使其无限期重复,直到建立连接以获得循环动画
另一种方法是进行顺序转换
,该转换将有两次0.5秒的暂停转换
顺便说一句,在代码中暂停主UI线程,这就是为什么看不到动画。您应该使用时间线
API制作动画。请看这里:
基本上,您只需定义距离为0.5秒的关键帧
s,然后设置文本值以添加另一个点。还可以使其无限期重复,直到建立连接以获得循环动画
另一种方法是进行顺序转换
,该转换将有两次0.5秒的暂停转换
顺便说一句,在代码中暂停主UI线程,这就是为什么无法看到动画。从其他线程运行<代码>任务
允许您在JavaFX应用程序线程上更新它的消息
属性,该属性应用于更新GUI,并且不能被长时间运行的任务阻止,因为它负责呈现:
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws InterruptedException {
updateMessage("Connecting.");
Thread.sleep(500);
updateMessage("Connecting..");
Thread.sleep(500);
updateMessage("Connecting...");
Thread.sleep(500);
return null;
}
};
// bind status to task's message
Status.get().bind(task.messageProperty());
// run task on different thread
new Thread(task).start();
Task Task=新任务(){
@凌驾
受保护的Void调用()引发InterruptedException{
更新消息(“连接”);
睡眠(500);
更新消息(“连接…”);
睡眠(500);
更新消息(“连接…”);
睡眠(500);
返回null;
}
};
//将状态绑定到任务的消息
Status.get().bind(task.messageProperty());
//在不同线程上运行任务
新线程(任务).start();
从其他线程运行<代码>任务
允许您在JavaFX应用程序线程上更新它的消息
属性,该属性应用于更新GUI,并且不能被长时间运行的任务阻止,因为它负责呈现:
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws InterruptedException {
updateMessage("Connecting.");
Thread.sleep(500);
updateMessage("Connecting..");
Thread.sleep(500);
updateMessage("Connecting...");
Thread.sleep(500);
return null;
}
};
// bind status to task's message
Status.get().bind(task.messageProperty());
// run task on different thread
new Thread(task).start();
Task Task=新任务(){
@凌驾
受保护的Void调用()引发InterruptedException{
更新消息(“连接”);
睡眠(500);
更新消息(“连接…”);
睡眠(500);
更新消息(“连接…”);
睡眠(500);
返回null;
}
};
//将状态绑定到任务的消息
Status.get().bind(task.messageProperty());
//在不同线程上运行任务
新线程(任务).start();
使用时间线
:使用时间线
: