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

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

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

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

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

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

我怎么能耽搁呢?

plupiseo

plupiseo1#

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

public static void main(String[] args) {
    System.out.println("test1");
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("test2");
}
wfveoks0

wfveoks02#

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

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

相关问题