为什么主要方法的类应该在javafx中公开?

时间:2021-05-22 21:21:07

here is a template code of netbeans IDE for a JavaFX project. if you clear public keyword from BombDroid class you get runtime error.why? Note:I know that main should be public.But why Bombdroid class should be public?

这是JavaFX项目的netbeans IDE的模板代码。如果从BombDroid类中清除public关键字,则会出现运行时错误。为什么?注意:我知道main应该是public.But为什么Bombdroid类应该公开?

package bombdroid;

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.shape.*;
import javafx.stage.Stage;

public class BombDroid extends Application {
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root);
        Line line = new Line(0, 0, 500, 500);
        Rectangle man = new Rectangle(0, 0, 100, 100);
        root.getChildren().add(line);
        root.getChildren().add(man);
        primaryStage.setScene(scene);
        primaryStage.show();
        }
    public static void main(String[] args) {
        launch(args);
        }
    }

1 个解决方案

#1


3  

The launcher for JavaFX applications works via reflection, a consequence of which and the way it is used is that a JavaFX Application class must be declared public.

JavaFX应用程序的启动程序通过反射工作,其结果和使用方式是必须将JavaFX Application类声明为public。

The specific error message you get when running your sample (without a main method, which is unnecessary for a JavaFX application) is:

运行示例时获得的特定错误消息(没有主方法,这对于JavaFX应用程序是不必要的)是:

Exception in Application constructor
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class BombDroid
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodException: BombDroid.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getConstructor(Class.java:1825)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:818)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

As you can see, the system is looking for a no-argument constructor to your application class. Tracing through, the source code at LauncherImpl.java:818, is:

如您所见,系统正在为您的应用程序类寻找无参数构造函数。追踪LauncherImpl.java:818的源代码是:

Constructor<? extends Application> c = appClass.getConstructor();

The documentation for the getConstructor() method states:

getConstructor()方法的文档说明:

Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

返回一个Constructor对象,该对象反映此Class对象所表示的类的指定公共构造函数。

@throws NoSuchMethodException if a matching method is not found.

@throws NoSuchMethodException如果找不到匹配的方法。

If your application class is not public, it has no public constructor, and a NoSuchMethodException is returned when you try to launch it. Which is what is happening here, causing the launch of the Application to fail.

如果您的应用程序类不是公共的,则它没有公共构造函数,并且在您尝试启动它时会返回NoSuchMethodException。这就是正在发生的事情,导致应用程序的启动失败。

Aside

在旁边

Note the requirement that the Application class for JavaFX be public differs from a non-JavaFX application. For example, the simplest hello world program in Java does not require a public class to run, only a public main method.

请注意,JavaFX的Application类是公共的要求与非JavaFX应用程序不同。例如,Java中最简单的hello world程序不需要运行公共类,只需要公共main方法。

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("hello, world");
    }
}

#1


3  

The launcher for JavaFX applications works via reflection, a consequence of which and the way it is used is that a JavaFX Application class must be declared public.

JavaFX应用程序的启动程序通过反射工作,其结果和使用方式是必须将JavaFX Application类声明为public。

The specific error message you get when running your sample (without a main method, which is unnecessary for a JavaFX application) is:

运行示例时获得的特定错误消息(没有主方法,这对于JavaFX应用程序是不必要的)是:

Exception in Application constructor
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class BombDroid
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodException: BombDroid.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getConstructor(Class.java:1825)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:818)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

As you can see, the system is looking for a no-argument constructor to your application class. Tracing through, the source code at LauncherImpl.java:818, is:

如您所见,系统正在为您的应用程序类寻找无参数构造函数。追踪LauncherImpl.java:818的源代码是:

Constructor<? extends Application> c = appClass.getConstructor();

The documentation for the getConstructor() method states:

getConstructor()方法的文档说明:

Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

返回一个Constructor对象,该对象反映此Class对象所表示的类的指定公共构造函数。

@throws NoSuchMethodException if a matching method is not found.

@throws NoSuchMethodException如果找不到匹配的方法。

If your application class is not public, it has no public constructor, and a NoSuchMethodException is returned when you try to launch it. Which is what is happening here, causing the launch of the Application to fail.

如果您的应用程序类不是公共的,则它没有公共构造函数,并且在您尝试启动它时会返回NoSuchMethodException。这就是正在发生的事情,导致应用程序的启动失败。

Aside

在旁边

Note the requirement that the Application class for JavaFX be public differs from a non-JavaFX application. For example, the simplest hello world program in Java does not require a public class to run, only a public main method.

请注意,JavaFX的Application类是公共的要求与非JavaFX应用程序不同。例如,Java中最简单的hello world程序不需要运行公共类,只需要公共main方法。

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("hello, world");
    }
}