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
javafx.scene.layout.Pane cannot be cast to javafx.fxml.FXMLLoader
提问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.fxml
and LoginController
当前视图和控制器:login.fxml
和LoginController
Next step View and Controller: loggedWindow.fxml
and UserPanelController
.
下一步视图和控制器:loggedWindow.fxml
和UserPanelController
。
Now, I'm in LoginController
and trying to switch the scene to loggedWindow.fxml
sending to UserPanelController
a 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));