我有一个用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);
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
但是我在运行时收到以下消息:
$this->container->get('security.context')
我用AnchorPane root替换了父root,但我仍然遇到同样的错误。请帮忙。
答案 0 :(得分:0)
引起: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。