我对 JavaFX 很陌生。但我真的很想学。我知道如何使用 ActionEvent 调用方法,但是如果我想在启动应用程序后立即调用一个方法怎么办?通常,只有当您执行某个操作(例如按下按钮)时,这些方法才会被执行,但在这种情况下,我只想在启动时运行它。有人可以帮忙吗?
最佳答案
只需在应用程序的 start
方法中调用您想要调用的方法即可。
public class Main extends Application {
@Override
public void init() {
//you can call your method here but if you
//plan on doing stuff to the stage call it in the start method
}
@Override
public void start(Stage stage) throws Exception {
// call your method here
myMethod();
//show the application
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
public void myMethod() {
//do Stuff
}
}
您可以在 init()
方法中调用该方法,但不能对舞台或场景执行操作。
关于JAVAFX - 如何在应用程序启动时调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53471768/