JavaFX 8如何在几乎空的Application类中启动JavaFX Application线程? [英] How does JavaFX 8 start the JavaFX Application thread in a nearly empty Application class?

查看:555
本文介绍了JavaFX 8如何在几乎空的Application类中启动JavaFX Application线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我们有以下类:

Lets say, we have the following class:

import javafx.application.Application;
import javafx.stage.Stage;

public class Test extends Application
{
    public Test()
    {
        System.out.println("Constructor");
    }

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        System.out.println("start");
    }

    public static void main(String... args)
    {
        System.out.println("main");
    }
}

它来自应用但它不使用任何方法。通常你通过在main中调用 launch(args)来启动一个JavaFX应用程序。

It's derived from Application but it doesn't use any of its methods. Usually you start a JavaFX application by calling launch(args) in the main.

当我启动这个程序时只有输出是main,所以不调用构造函数和start,但程序不会因为运行JavaFX Application线程而终止。但是它来自哪里?

When I start this program the only output is "main", so the constructor and start aren't called, but the program doesn't terminate because there is a JavaFX Application thread running. But where does it come from?

我做了一些调试,发现线程是在main方法运行之前从主线程启动的。堆栈跟踪以 NativeMethodAccessorImpl 开始。

I did some debugging and found out that the thread is started from the main thread before the main method runs. The stack trace starts with NativeMethodAccessorImpl.

为了更加奇怪:当我从另一个类启动main方法时,JavaFX应用程序线程未启动:

To get even weirder: when I start the main method from a different class, the JavaFX Application thread isn't started:

public class Test2
{
    public static void main(String[] args)
    {
        Test.main(args);
    }
}

那么这是什么样的黑魔法?

So what kind of black magic is this?

推荐答案

Java使用不同的方法启动这两个应用程序。

Java uses different approaches to launch the two applications.

尝试运行以下代码:

public class Test3 {

    public static void main(String[] args) {

        Class<?> actualMainClassTest = LauncherHelper.checkAndLoadMain(true, 1, Test.class.getName());
        Class<?> actualMainClassTest2 = LauncherHelper.checkAndLoadMain(true, 1, Test2.class.getName());

        System.out.println("Actual loaded main class for Test: " + actualMainClassTest.getName());
        System.out.println("Actual loaded main class for Test2: " + actualMainClassTest2.getName());
    }
}

输出


  • Test的实际加载主类:sun.launcher.LauncherHelper $ FXHelper

  • Test2的实际加载主类:Test2

您可以看到 Test2 类的实际加载主类是 Test2 ,但加载的主要类
测试 sun.launcher.LauncherHelper $ FXHelper

You can see that the actual loaded main class for the Test2 class is Test2, but the loaded main class for Test is sun.launcher.LauncherHelper$FXHelper.

这是因为Java启动程序检查要启动的主类是否是 javafx.application的子类。应用
如果是,它会加载 sun.launcher.LauncherHelper $ FXHelper 的主方法,它会调用JavaFX应用程序的启动程序方法
com.sun.javafx.application.LauncherImpl#launchApplication )。

This happens because the Java launcher checks if the main class to be launched is a subclass of javafx.application.Application. If it is, it loads the main method of sun.launcher.LauncherHelper$FXHelper instead, which invokes a launcher method for JavaFX applications (com.sun.javafx.application.LauncherImpl#launchApplication).

此方法负责启动JavaFX应用程序。 在启动应用程序后调用#main

This method is responsible for launching the JavaFX application. Test#main is called after the application is launched:

测试#main 由Test2调用,不使用FX启动器,因为Test2不是 javafx.application.Application 的子类。

When Test#main is called by Test2, the FX launcher is not used because Test2 is not a subclass of javafx.application.Application.

这篇关于JavaFX 8如何在几乎空的Application类中启动JavaFX Application线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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