使用JavaFX时Application构造函数中的异常 [英] Exception in Application constructor when using JavaFX

查看:413
本文介绍了使用JavaFX时Application构造函数中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习JavaFX,并从一本使用JDK 9的书中获得了此代码.运行该程序时,我遇到了错误.我查看了其他代码,并尝试将'@Override'和"throws Exception"添加到start方法中,但是我遇到了相同的错误.生成程序时没有错误.

I recently started learning JavaFX, and got this code from a book that is using JDK 9. When I ran the program I got errors. I looked at other code and tried adding '@Override' and "throws Exception" to the start method, however I got the same errors. I get no errors when building the program.

如果有帮助:我正在使用JDK 11.0.2,以及来自openjfx.io的JavaFX.

If it helps: I am using JDK 11.0.2, and JavaFX from openjfx.io.

import javafx.application.*;
import javafx.scene.*;
importjavafx.stage.*;
import javafx.scene.layout.*;

class JavaFXSkel extends Application{

public static void main(String[] args){
    System.out.println("Launching JavaFX Application");

    launch(args);
}

public void init(){
    System.out.println("Inside the init() method");
}


public void start(Stage myStage){
    System.out.println("Inside the start() method");

    myStage.setTitle("JavaFX Skeleton");

    //makes a root node with a flow layout pane
    FlowPane rootNode = new FlowPane();

    //Crate a scne
    Scene myScene = new Scene(rootNode, 300, 200);

    //Set teh scene on stage
    myStage.setScene(myScene);

    //Show the stage and the scene
    myStage.show();
}

public void stop(){
    System.out.println("Inside the stop() method");
}
}

错误消息:

Exception in Application constructor
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class JavaFXSkel
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:890)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NoSuchMethodException: JavaFXSkel.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3350)
at java.base/java.lang.Class.getConstructor(Class.java:2152)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:801)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)

推荐答案

您的JavaFXSkel类必须为public,如

Your JavaFXSkel class must be public, as stated in the documentation of Application:

Application子类必须声明为public,并且必须具有公共的无参数构造函数.

The Application subclass must be declared public and must have a public no-argument constructor.

JavaFX运行时使用反射实例化Application子类的实例.它通过使用该类的公共no-arg构造函数来完成此操作.但是,您的JavaFXSkel类没有显式声明任何构造函数,这意味着它具有隐式默认构造函数.默认构造函数与封闭类具有相同的访问修饰符,这是您所用的默认程序包访问.在§8.8中提到Java语言规范 1 的.9 :

The JavaFX runtime instantiates an instance of your Application subclass using reflection. It does this by using the public no-arg constructor of the class. However, your JavaFXSkel class doesn't explicitly declare any constructors which means it has the implicit default constructor. The default constructor has the same access modifier as the enclosing class, which is the default package access in your case. This is mentioned in §8.8.9 of the Java Language Specification1:

如果一个类不包含构造函数声明,则默认构造函数被隐式声明.顶级类,成员类或本地类的默认构造函数的形式如下:

If a class contains no constructor declarations, then a default constructor is implicitly declared. The form of the default constructor for a top level class, member class, or local class is as follows:

  • 默认构造函数具有与类相同的访问修饰符,除非该类缺少访问修饰符,在这种情况下,默认构造函数具有程序包访问权限(默认构造函数没有形式参数,但在非private内部成员类中除外,其中默认构造函数隐式声明一个表示该类立即封闭实例的形式参数(§15.9.2§15.9.3).

    The default constructor has no formal parameters, except in a non-private inner member class, where the default constructor implicitly declares one formal parameter representing the immediately enclosing instance of the class (§8.8.1, §15.9.2, §15.9.3).

    这意味着您的JavaFXSkel没有 public 无参数构造函数,因此没有NoSuchMethodException.

    What this means is your JavaFXSkel has no public no-arg constructor, thus the NoSuchMethodException.

    1.不要指望初学者阅读过多的JLS,如果有的话.我只是提供它作为官方参考.

    这篇关于使用JavaFX时Application构造函数中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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