【问题标题】:How do I use more than one fxml with javaFX?如何在 javaFX 中使用多个 fxml?
【发布时间】:2015-10-17 21:55:23
【问题描述】:

目前,我的主要 javaFX 主类看起来像这样:

public class Main extends Application {

private Stage primaryStage;
private AnchorPane rootLayout;
private Shell shl = new Shell();;
@FXML
private PasswordField pass;
@FXML
private TextField user;
@FXML
private String description;
@FXML
private String title;


@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("WorkFlow Manager");
    initRootLayout();
}

/**
 * root layout (base layout).
 */
public void initRootLayout() {
    try {

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("LoginMenu.fxml"));
        rootLayout = (AnchorPane) loader.load();
        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);                                    
        primaryStage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void toDoLayout() {
    try{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("Todo.fxml"));
        AnchorPane toDo = (AnchorPane) loader.load();
        Scene scene = new Scene(rootLayout);

    }catch(IOException e) {
        e.printStackTrace();
    }
}

/**
 *
 * @return primaryStage
 */
public Stage getPrimaryStage() {
    return primaryStage;
}

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

@FXML
private void login(ActionEvent event) {
    if(user.getText()!=null && pass.getText()!=null){
        shl.login(user.getText(),pass.getText());
        System.out.printf("%s",shl.getCurrentUser());
        //toDoLayout(); I commented this out since it isn't working
    }
}
}

我的第一个函数 initRootLayout() 工作正常,按预期加载 fxml 并在操作时执行预期的函数。

但是,我正在尝试的是启动一个新场景(替换登录 fxml 场景)。我尝试调用 toDoLayout() 函数(它已被注释掉),因为每次单击按钮时它都会抛出这些异常:

 java.lang.ClassNotFoundException: main.iface.MainTerminal{
/C:/Users/a51217/workspace/wfmgr/bin/main/iface/Todo.fxml:9
  at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
  at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
  at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
  at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
  at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
  at javafx.fxml.FXMLLoader.load(Unknown Source)
  at javafx.fxml.FXMLLoader.load(Unknown Source)
  at main.iface.Main.toDoLayout(Main.java:59)
  at main.iface.Main.login(Main.java:84)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at sun.reflect.misc.Trampoline.invoke(Unknown Source)
  at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
  at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
  at javafx.event.Event.fireEvent(Unknown Source)
  at javafx.scene.Node.fireEvent(Unknown Source)
  at javafx.scene.control.Button.fire(Unknown Source)
  at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
  at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
  at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
  at javafx.event.Event.fireEvent(Unknown Source)
  at javafx.scene.Scene$MouseHandler.process(Unknown Source)
  at javafx.scene.Scene$MouseHandler.process(Unknown Source)
  at javafx.scene.Scene$MouseHandler.access$1900(Unknown Source)
  at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
  at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
  at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
  at com.sun.glass.ui.View.notifyMouse(Unknown Source)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
  at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)

javafx.fxml.LoadException: java.lang.ClassNotFoundException: main.iface.MainTerminal{
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
    at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at main.iface.Main.toDoLayout(Main.java:59)
    at main.iface.Main.login(Main.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Node.fireEvent(Unknown Source)
    at javafx.scene.control.Button.fire(Unknown Source)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.access$1900(Unknown Source)
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: main.iface.MainTerminal{
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 63 more

编辑:我的 Todo.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.iface.MainTerminal{">
   <children>
      <ListView layoutX="37.0" layoutY="47.0" prefHeight="333.0" prefWidth="195.0" />
      <ScrollBar layoutX="37.0" layoutY="47.0" orientation="VERTICAL" prefHeight="333.0" prefWidth="14.0" />
      <TextField fx:id="getTitle" layoutX="262.0" layoutY="47.0" prefHeight="25.0" prefWidth="321.0" />
      <TextArea fx:id="getDescription" layoutX="262.0" layoutY="92.0" prefHeight="247.0" prefWidth="321.0" />
      <Button layoutX="262.0" layoutY="355.0" mnemonicParsing="false" text="Create Task" />
      <Button layoutX="351.0" layoutY="355.0" mnemonicParsing="false" text="Assign User to this Task" />
      <ScrollBar layoutX="262.0" layoutY="92.0" orientation="VERTICAL" prefHeight="247.0" prefWidth="14.0" />
   </children>
</AnchorPane>

MainTerminal 不是我的控制器,我忘记更正了。我的 Main 充当控制器,所以我将第 9 行更改为:

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.iface.Main{">

在此更改之后,例外情况似乎相同:

java.lang.ClassNotFoundException: main.iface.Main{
/C:/Users/a51217/workspace/wfmgr/bin/main/iface/Todo.fxml:9
  at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
  at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
  at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
  at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
  at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
  at javafx.fxml.FXMLLoader.load(Unknown Source)
  at javafx.fxml.FXMLLoader.load(Unknown Source)
  at main.iface.Main.toDoLayout(Main.java:59)
  at main.iface.Main.login(Main.java:84)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at sun.reflect.misc.Trampoline.invoke(Unknown Source)
  at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
  at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
  at javafx.event.Event.fireEvent(Unknown Source)
  at javafx.scene.Node.fireEvent(Unknown Source)
  at javafx.scene.control.Button.fire(Unknown Source)
  at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
  at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
  at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
  at javafx.event.Event.fireEvent(Unknown Source)
  at javafx.scene.Scene$MouseHandler.process(Unknown Source)
  at javafx.scene.Scene$MouseHandler.process(Unknown Source)
  at javafx.scene.Scene$MouseHandler.access$1900(Unknown Source)
  at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
  at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
  at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
  at com.sun.glass.ui.View.notifyMouse(Unknown Source)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
  at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)

javafx.fxml.LoadException: java.lang.ClassNotFoundException: main.iface.Main{
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
    at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at main.iface.Main.toDoLayout(Main.java:59)
    at main.iface.Main.login(Main.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Node.fireEvent(Unknown Source)
    at javafx.scene.control.Button.fire(Unknown Source)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.access$1900(Unknown Source)
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: main.iface.Main{
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 63 more

【问题讨论】:

  • 看起来它找不到你的控制器类。 ToDo.fxml 第 9 行是什么?
  • 我有这个:javafx.com/javafx/8.0.40" xmlns:fx="javafx.com/fxml/1" fx:controller="main.iface.MainTerminal{">
  • 您不认为fx:controller 属性中的流浪{ 可能是问题所在?
  • 异常清楚地表明它找不到控制器类。此外,您的 toDoLayout() 方法会创建一个 Scene,但随后什么也不做。您需要在舞台上显示它。最后,您的代码有一些非常奇怪的地方,因为您似乎将 Application 子类用作控制器,这实际上不起作用。
  • 是的,您使用Application 子类和覆盖start() 方法来启动您的应用程序,但您不应该使用与控制器相同的类;您应该将控制器代码放在一个单独的类中。 (这不是导致您的问题的原因,这又是因为您以某种方式错误地类名,但它会在以后引起问题。)

标签: java javafx fxml


【解决方案1】:

发生异常是因为您在fx:controller 属性中有一个额外的{。此属性应设置为控制器类的完全限定名称。由于{ 不是类名中的合法字符,fx:controller = "main.iface.MainTerminal{" 不可能是正确的。

假设您的控制器类是 MainTerminal 并且位于名为 main.iface 的包中,则正确的属性设置是 fx:controller = "main.iface.MainTerminal"

此外,您的 toDoLayout() 方法只是创建了一个包含在 ToDo.fxml 中定义的 UI 的 Scene,但对该场景没有任何作用:您需要在舞台中显示它。因此,一旦您修复了 fx:controller 属性,它就会正常运行,但在您修复该代码之前不会执行任何操作。

最后,看起来您正在使用您的 Application 子类作为(至少?)您的一个 FXML 文件的控制器,因为其中有 @FXML-annotated 字段。不要不要这样做:这会非常混乱。 (发生的情况是,您的 Main 类的一个实例在启动时为您创建,然后 另一个实例FXMLLoader 为将其声明为控制器的任何 FXML 文件创建。一些字段将在第一个实例中初始化 - primaryStagerootLayout - 其他字段(@FXML-注释的字段)将在第二个实例中初始化。跟踪初始化的内容将非常非常困难null 仍然是什么。)您应该创建 Application 子类来启动应用程序,然后每个 FXML 都应该有自己的独立控制器类。

【讨论】:

  • 感谢您的信息!我将尝试解决您谈到的所有问题,看看它们是否是问题的根源。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-20
  • 2022-11-13
  • 2014-08-17
  • 1970-01-01
相关资源
最近更新 更多