繁体   English   中英

JAVAFX-如何在应用程序启动时调用方法

[英]JAVAFX - How to call a method on launch of the application

关于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()方法内调用该方法,但不能对舞台或场景做任何事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM