簡體   English   中英

無法更正FXML加載的javafx主要方法中的錯誤

[英]Error in javafx main method for FXML loading not getting rectified

我有一個使用javafx創建的可運行的計算器程序。 但是,為了改進程序,我決定在輸入數字時也包含鍵盤事件。 為此,我必須調用我的Controller類的一個實例,以從主類傳遞一個參數,在該類中將keyevent記錄到FXMLController中。 但是,在我修改了代碼之后,它沒有在我的主類中編譯以顯示異常。 它使AnchorPane無法轉換為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$000(LauncherImpl.java:56)
at com.sun.javafx.application.LauncherImpl$1.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$8.run(LauncherImpl.java:837)
at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.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.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
... 1 more
    Exception running application mycalc.Mycalc

我也用AnchorPane根替換了Parent根,但仍然遇到相同的錯誤。 請幫忙。

造成原因:java.lang.ClassCastException:javafx.scene.layout.AnchorPane無法轉換為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文件。 它不返回FXMLLoader。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM