gpt4 book ai didi

java - 使用 JavaFX 时应用程序构造函数中出现异常

转载 作者:行者123 更新时间:2023-12-01 17:46:45 24 4
gpt4 key购买 nike

我最近开始学习JavaFX,并从一本使用JDK 9的书中获得了这段代码。当我运行该程序时,我遇到了错误。我查看了其他代码并尝试将“@Override”和“抛出异常”添加到启动方法中,但是我遇到了相同的错误。构建程序时我没有收到任何错误。

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

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,如 Application 的文档中所述。 :

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

JavaFX 运行时使用反射实例化您的 Application 子类的实例。它通过使用类的公共(public)无参数构造函数来实现此目的。但是,您的 JavaFXSkel 类没有显式声明任何构造函数,这意味着它具有隐式默认构造函数。默认构造函数具有与封闭类相同的访问修饰符,这是您的情况下的默认包访问权限。 §8.8.9中提到了这一点Java 语言规范1:

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:

  • The default constructor has the same access modifier as the class, unless the class lacks an access modifier, in which case the default constructor has package access (§6.6).

  • 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

<小时/>

1。不要期望初学者读过太多 JLS 的内容(如果有的话);我只是提供它作为官方引用。

关于java - 使用 JavaFX 时应用程序构造函数中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54353822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com