【发布时间】:2019-01-25 04:25:33
【问题描述】:
我最近开始学习 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)
【问题讨论】:
-
您的应用程序的依赖项似乎存在冲突。你在使用一些依赖管理的工具吗?
-
尝试在
start()和init()上同时抛出@Override和throws Exception -
添加
@Override和throws Exception无效。至于依赖管理,我没有使用任何工具。如果有帮助,我正在使用 Intellij -
您的第三次导入看起来很可疑。
importjavafx.stage.*; -
@SAIFULHAQUE 检查此链接openjfx.io/openjfx-docs