我有一个用 javafx 创建的可用计算器程序。然而,为了改进我的程序,我决定将按键事件也包含在输入数字中。为此,我必须调用 Controller 类的实例来将参数从主类传递给我的 FXMLController,在主类中我将 keyevent 记录到了其中。但是,在我修改代码后,它没有编译,在我的主类中显示异常。 它给出 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 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 文件。它不返回 FXMLLoader。
关于java - FXML 加载的 javafx main 方法中的错误未得到纠正,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34614142/