java - JAVAFX - 如何在启动应用程序时调用方法
问题描述
我是 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()
但不能对舞台或场景做任何事情。
推荐阅读
- python - 无法通过 Q-learning 实现购物车问题
- reactjs - 在 React 中刷新页面后,如何重新连接到后端的服务器端事件?
- javascript - 使用 moment.js 获取未来 7 天的问题
- minecraft - Fabric 两次注册一个项目
- angular - 如何编写 json pie 角度获取属性?
- php - 根据其值替换一个 PHP 变量名
- reactjs - 传递状态与 useState 反应
- css - 如何将主文件夹中的变量作为 React 中 module.css 中的属性传递
- python - 使用python应用梯形法的一个问题的解决方案
- java - 无法将数据添加到数据库,找不到 404 进行映射