设置 root 的 JavaFx FXML 加载文件问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23729277/
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 FXML load file issues with setting root
提问by Nrandazzo
New to javaFx and wanting to use scenebuilder for GUI development, i've come across an issue with no luck searching the website nor the web in general for my problem, although similar questions have been asked, thought a different perspective could be needed. I am trying to load an FXML file through Netbeans after a quick build to test functionality so the code is simple, but i cannot get the root file to be set in the controller. my code is the following public class Divergex extends Application {
我是 javaFx 的新手并希望使用 Scenebuilder 进行 GUI 开发,但我遇到了一个问题,在网站或网络上搜索我的问题时没有运气,尽管已经提出了类似的问题,但我认为可能需要不同的视角。我试图在快速构建后通过 Netbeans 加载 FXML 文件以测试功能,因此代码很简单,但我无法在控制器中设置根文件。我的代码是以下公共类 Divergex extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("DivergexGUI.fxml"));
Scene scene = new Scene(root);
scene.setRoot(root);
stage.setScene(scene);
stage.show();
}
Ive tried suggestions in changing fxroot to a Vbox with no luck, i continue to get a Load exception on the compile :
我已经尝试过将 fxroot 更改为 Vbox 的建议,但没有运气,我继续在编译时遇到 Load 异常:
Exception in Application start method... Caused by: javafx.fxml.LoadException: Root hasn't been set. Use method setRoot() before load.
应用程序启动方法中的异常... 原因:javafx.fxml.LoadException:尚未设置根。在加载之前使用方法 setRoot()。
yet when i use
然而当我使用
scene.setRoot(root);
the same exception is experienced
遇到同样的异常
i've narrowed the issue down to the fact that my FXML file is unable to be set as a root in the Parent object but have had no luck in tackling this. Any suggestions would be great thanks.
我已将问题缩小到我的 FXML 文件无法在 Parent 对象中设置为根的事实,但没有解决这个问题。任何建议都会非常感谢。
采纳答案by James_D
<fx:root>
specifies a "dynamic root" for your FXML file; this means the root of the FXML file is an object that is set on the loader prior to loading the file. This is typically used for custom controls, where you want the control to be a subclass of Node
that can be instantiated using regular Java code, but want to define its layout using FXML. Proper use of <fx:root>
(or at least an example of how it can be used) is shown in the standard documentation. In particular, if you use <fx:root>
you must:
<fx:root>
为您的 FXML 文件指定一个“动态根”;这意味着 FXML 文件的根是一个在加载文件之前在加载器上设置的对象。这通常用于自定义控件,您希望控件成为Node
可以使用常规 Java 代码实例化的子类,但希望使用 FXML 定义其布局。标准文档中<fx:root>
显示了正确使用(或至少是如何使用它的示例)。特别是,如果您使用,您必须:<fx:root>
- Create an
FXMLLoader
instance, instead of using the static convenienceFXMLLoader.load(URL)
method - Call setRoot(...) on that instance, and pass in the object that is to be the root of the FXML.
- 创建一个
FXMLLoader
实例,而不是使用静态方便的FXMLLoader.load(URL)
方法 - 在该实例上调用 setRoot(...),并传入将成为 FXML 根的对象。
For standard FXML use, you just use a regular instance declaration as the root. Almost every example available works this way: probably the best place to start is the official tutorial. In your case, since you want a VBox
, you probably just need
对于标准 FXML 使用,您只需使用常规实例声明作为根。几乎所有可用的示例都是这样工作的:可能最好的起点是官方教程。在您的情况下,由于您想要一个VBox
,您可能只需要
<VBox xmlns="javafx.com/javafx/8"; xmlns:fx="javafx.com/fxml/1"; fx:controller="divergex.DivergexGUIController">
<!-- ... -->
</VBox>
EditIf Netbeans is giving you issues, I recommend using Eclipse with the e(fx)clipse plugin. There's a very barebones, but pretty much all you need, tutorial.
编辑如果 Netbeans 给您带来问题,我建议将 Eclipse 与e(fx)clipse 插件一起使用。有一个非常准系统,但几乎所有你需要的,教程。
回答by dam van tai
uncheck id::root in scence builder or change id::root to vbox
在场景生成器中取消选中 id::root 或将 id::root 更改为 vbox
回答by fahed ghodhbane
Just you should not use fx:root construct in scene builder. so remove this line of code from the fxml file.
只是您不应该在场景构建器中使用 fx:root 构造。所以从 fxml 文件中删除这行代码。
For example: <fx:root prefHeight="246.0" prefWidth="479.0" type="AnchorPane" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.mycontroller">
例如: <fx:root prefHeight="246.0" prefWidth="479.0" type="AnchorPane" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.mycontroller">