当start()已经存在时,为什么在JavaFX Application中使用主要方法 [英] Why is main method used in JavaFX Application when start() already exist

查看:74
本文介绍了当start()已经存在时,为什么在JavaFX Application中使用主要方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaFX应用程序的起点是启动方法.但是在示例JavaFX应用程序中,也包含一个主要方法.在这种特殊情况下main方法的用途是什么,为什么需要将start()定义为JavaFX的起点.我们不能简单地使用main方法来定义诸如Swings的起点吗?

The starting point for a JavaFX application is start method. But in the sample JavaFX applications, there is a main method included as well. What is the use of main method in this particular case and why was there a need to define start() as starting point for JavaFX. Couldn't we simply use the main method to define a starting point like Swings?

一个示例HelloWorld应用程序:

A sample HelloWorld application:

public class HelloWorld extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button("Hello World");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

推荐答案

来自 Oracle文件

当JAR出现时,JavaFX应用程序不需要main()方法应用程序的文件是使用JavaFX Packager工具创建的,将JavaFX Launcher嵌入JAR文件中.但是,这是有用以包含 main()方法,因此您可以运行以前的在没有JavaFX Launcher的情况下创建的,例如在JavaFX工具尚未完全集成.另外,秋千嵌入JavaFX代码的应用程序需要main()方法.

The main() method is not required for JavaFX applications when the JAR file for the application is created with the JavaFX Packager tool, which embeds the JavaFX Launcher in the JAR file. However, it is useful to include the main() method so you can run JAR files that were created without the JavaFX Launcher, such as when using an IDE in which the JavaFX tools are not fully integrated. Also, Swing applications that embed JavaFX code require the main() method.

这篇关于当start()已经存在时,为什么在JavaFX Application中使用主要方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆