Lal*_*nci 4 java javafx java-8 javafx-8
使用JavaFX,单击按钮我想这样做:
spinBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
field.setDisable(false);
//Delay for 10 seconds
field.setDisable(true);
}
});
Run Code Online (Sandbox Code Playgroud)
我很快意识到睡眠不起作用,因为它完全冻结了GUI.我也试过睡眠线程来获得一个计时器,但如果输入我希望延迟,它仍会冻结GUI.(以下示例)
spinBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
ExampleTimerThread exampleSleepyThread = new ExampleTimerThread();//this extends Thread
exampleSleepyThread.start();
//thread sleeps for 10 secs & sets public static boolean finished = true; after sleep
while(finished == true){
field.setDisable(false);
}
}
});
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能防止此代码冻结GUI?我知道在Swing中,有一个计时器.JavaFX中有类似的东西吗?
使用PauseTransition延迟事件:
spinBtn.setOnAction(e -> {
field.setDisabled(false);
PauseTransition pt = new PauseTransition(Duration.seconds(10));
pt.setOnFinished(ev -> {
field.setDisabled(true);
});
pt.play();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
91 次 |
| 最近记录: |