actionevent中的javafx thread.sleep()或pause()

3df52oht  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(501)

我是javafx新手,每次按下按钮都会尝试,首先,它会在标签上显示一些信息,然后更改场景。其实一切都还可以,但我就是找不到如何等待一个特定的时间量之前,改变现场。
我尝试了thread.sleep()如下:(它正确地等待,但不知何故它不会更改标签的文本)

  1. @FXML
  2. public void pressButton(ActionEvent event) throws IOException, InterruptedException {
  3. user = new User(inUsername.getText(),inPassword.getText());
  4. lLeftBottom.setText(user.getUserInfo());
  5. Thread.sleep(2000);
  6. changeScene2(event);
  7. }

(编辑,感谢slaw解决了pause()的actionevent问题)
我也尝试过javafx的pause方法,但它不会等待,仍然会立即跳转到另一个场景

  1. @FXML
  2. public void pressButton(ActionEvent event) throws IOException, InterruptedException {
  3. user = new User(inUsername.getText(),inPassword.getText());
  4. PauseTransition pause = new PauseTransition(Duration.seconds(3));
  5. pause.setOnFinished(e ->{
  6. lLeftBottom.setText(user.getUserInfo());
  7. });
  8. pause.play();
  9. changeScene2(event);
  10. }

我怎么能耽搁呢?

plupiseo

plupiseo1#

关于你的第一个问题:试着用try-catch。为我工作。

  1. public static void main(String[] args) {
  2. System.out.println("test1");
  3. try {
  4. Thread.sleep(2000);
  5. } catch (InterruptedException e) {
  6. // TODO Auto-generated catch block
  7. e.printStackTrace();
  8. }
  9. System.out.println("test2");
  10. }
wfveoks0

wfveoks02#

你已经向后使用了暂停转换。如果要在暂停后更改场景,则需要在onfinished事件处理程序中包含该部分:

  1. @FXML
  2. public void pressButton(ActionEvent event) throws IOException, InterruptedException {
  3. user = new User(inUsername.getText(),inPassword.getText());
  4. PauseTransition pause = new PauseTransition(Duration.seconds(3));
  5. pause.setOnFinished(e ->{
  6. changeScene2(event);
  7. });
  8. lLeftBottom.setText(user.getUserInfo());
  9. pause.play();
  10. }

相关问题