【发布时间】:2018-07-13 20:52:11
【问题描述】:
我想实现这样的事情:用户按下登录按钮,然后标签显示:
“连接。”
0.5 秒时间间隔
“正在连接..”
0.5 秒时间间隔
“正在连接……”
等等
只是一种视觉效果,表明某事实际上正在“幕后”进行。
我设法得到的并不是我所期望的。我单击按钮,等待 1.5 秒,然后我得到“正在连接...”,缺少前面的 2 个步骤。
首先,我的Status类
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:stackoverflow.com/questions/9966136/…
标签: multithreading javafx