java javafx.scene.layout.Pane 不能转换为 javafx.fxml.FXMLLoader

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27556536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:59:21  来源:igfitidea点击:

javafx.scene.layout.Pane cannot be cast to javafx.fxml.FXMLLoader

javajavafx

提问by Shmwel

I'm having trouble when trying to switch from a scene to another scene. The scenario is this:

尝试从一个场景切换到另一个场景时遇到问题。场景是这样的:

Current View and Controller: login.fxmland LoginController

当前视图和控制器login.fxmlLoginController

Next step View and Controller: loggedWindow.fxmland UserPanelController.

下一步视图和控制器loggedWindow.fxmlUserPanelController



Now, I'm in LoginControllerand trying to switch the scene to loggedWindow.fxmlsending to UserPanelControllera parameter, but when I'm using my code I get:

现在,我正在LoginController尝试将场景切换为loggedWindow.fxml发送到UserPanelController参数,但是当我使用我的代码时,我得到:

javafx.scene.layout.Pane cannot be cast to javafx.fxml.FXMLLoader

LoginController:

登录控制器:

FXMLLoader loggedWindow = null;
loggedWindow = FXMLLoader.load(getClass().getResource("loggedWindow.fxml")); // here crashes!
Pane root = loggedWindow.load();

UserPanelController controller = loggedWindow.getController();
controller.initData(customer);

Stage switchScene = (Stage)((Node)event.getSource()).getScene().getWindow();
switchScene.setResizable(false);
switchScene.setTitle("Welcome " + customer.FirstName + " " + customer.LastName);
switchScene.setScene(new Scene(root, 800, 500));

switchScene.show();

LoggedWindow.fxml

记录窗口.fxml

<Pane maxHeight="500.0" maxWidth="800.0" minHeight="500.0" minWidth="800.0" prefHeight="500.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Main.UserPanelController">
   <children>
      <ToolBar prefHeight="40.0" prefWidth="831.0">
        <items>
         .
         .
         stuff (buttons/labels and so on).
         .
         .
</Pane>

I would appreciate any help! Thanks in advance.

我将不胜感激任何帮助!提前致谢。

Update 1

更新 1

Also took in consideration this reference: Accessing FXML controller class

还考虑了这个参考:访问 FXML 控制器类

回答by VinceOPS

You're using the "load" method of FXMLLoader which returns the root node of your .fxml file. In that case, it's returning your Pane.

您正在使用 FXMLLoader 的“加载”方法,该方法返回 .fxml 文件的根节点。在这种情况下,它会返回您的窗格。

You should use it to create your scene!

你应该用它来创建你的场景!

See the example given in the JavaFX tutorial, like:

请参阅 JavaFX 教程中给出的示例,例如:

Pane root = FXMLLoader.load(getClass().getResource("loggedWindow.fxml"));
Scene scene = new Scene(root, width, height, color);

Other way, taken from one of my old code, using non-static FXMLLoader:

其他方式,取自我的旧代码之一,使用非静态 FXMLLoader:

FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlFile));
Parent root;

try {
    root = loader.load();
} catch (IOException ioe) {
    // log exception
    return;
}

// Color.TRANSPARENT allows use of rgba colors (alpha layer)
setScene(new Scene(root, Color.TRANSPARENT));