用于 FXML 加载的 javafx 主要方法中的错误未得到纠正

Error in javafx main method for FXML loading not getting rectified

我有一个用 javafx 创建的工作计算器程序。但是,为了改进我的程序,我决定也包括 keyevents 来输入数字。为此,我不得不调用我的控制器 class 的一个实例,以将参数从主 class 传递到我的 FXMLController 记录键事件的地方。然而,在我修改代码后,它没有编译,在我的 main class 中显示异常。 它给出 AnchorPane cannot be cast to javafx.fxml.FXMLLoader

这是以前的工作代码:

public class Mycalc extends Application {

@Override
public void start(Stage stage) throws Exception {

        stage.initStyle(StageStyle.TRANSPARENT);
        Parent root =FXMLLoader.load(getClass().getResource("calc1.fxml"));
        Scene scene = new Scene(root,Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
    launch(args);
 }

}

这是当前代码(有错误):

public class Mycalc extends Application {

@Override
public void start(Stage stage) throws Exception {
    stage.initStyle(StageStyle.TRANSPARENT);

    FXMLLoader loader= FXMLLoader.load(getClass().getResource("calc1.fxml"));
    Parent root = loader.load();


    Scene scene = new Scene(root,Color.TRANSPARENT);

    stage.setScene(scene);
    stage.show();
    calc1Controller controller= loader.getController();
    scene.setOnKeyPressed(new EventHandler<KeyEvent>() 
    {
        @Override
        public void handle(KeyEvent keyEvent) 
        {

            switch (keyEvent.getCode()) 
            {
                case DIGIT1:
                    controller.handleDigit("1");
                    break ;
                case DIGIT2:
                    controller.handleDigit("2");
                    break ;
                case DIGIT3:
                    controller.handleDigit("3");
                    break ;
                case DIGIT4:
                    controller.handleDigit("4");
                    break ;
                case DIGIT5:
                    controller.handleDigit("5");
                    break ;
                case DIGIT6:
                    controller.handleDigit("6");
                    break ;
                case DIGIT7:
                    controller.handleDigit("7");
                    break ;
                case DIGIT8:
                    controller.handleDigit("8");
                    break ;
                case DIGIT9:
                    controller.handleDigit("9");
                    break ;
                case ADD:
                    controller.handleOperatorkey("+");
                    break ;
                case SUBTRACT:
                    controller.handleOperatorkey("-");
                    break ;
                case MULTIPLY:
                    controller.handleOperatorkey("*");
                    break ;
                case DIVIDE:
                    controller.handleOperatorkey("/");
                    break ;
                case EQUALS:
                    controller.handleEqualKey();
                    break ;
            }
        }   
    });
}

但是当 运行 时我收到以下消息:

   Exception in Application start method
   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:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
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:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
    Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
at com.sun.javafx.application.LauncherImpl.access[=12=]0(LauncherImpl.java:56)
at com.sun.javafx.application.LauncherImpl.run(LauncherImpl.java:158)
at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.ClassCastException: javafx.scene.layout.AnchorPane cannot be cast to javafx.fxml.FXMLLoader
at mycalc.Mycalc.start(Mycalc.java:32)
at com.sun.javafx.application.LauncherImpl.run(LauncherImpl.java:837)
at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:335)
at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:298)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access0(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication.run(WinApplication.java:112)
... 1 more
    Exception running application mycalc.Mycalc

我也用 AnchorPane root 替换了 Parent root 但我仍然遇到同样的错误。请帮忙。

Caused by: java.lang.ClassCastException: javafx.scene.layout.AnchorPane cannot be cast to javafx.fxml.FXMLLoader

而不是:

FXMLLoader loader= FXMLLoader.load(getClass().getResource("calc1.fxml"));
Parent root = loader.load();

尝试(未测试)

FXMLLoader loader= new FXMLLoader(getClass().getResource("calc1.fxml"));
Parent root = loader.load();

或(如果您的 FXML 根元素是 AnchorPane):

FXMLLoader loader= new FXMLLoader(getClass().getResource("calc1.fxml"));
AnchorPane root = loader.load();

FXMLLoader.load 加载 FXML 文件。它不是 return FXMLLoader。