设置 root 的 JavaFx FXML 加载文件问题 [英] JavaFx FXML load file issues with setting root

查看:29
本文介绍了设置 root 的 JavaFx FXML 加载文件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javaFx 的新手并想使用 Scenebuilder 进行 GUI 开发,我遇到了一个问题,在网站或网络上搜索我的问题时都没有运气,尽管有人问过类似的问题,但我认为可能有不同的观点需要.我试图在快速构建后通过 Netbeans 加载 FXML 文件以测试功能,因此代码很简单,但我无法在控制器中设置根文件.我的代码如下公共类 Divergex 扩展应用程序{

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 {

@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();
}

我尝试了将 fxroot 更改为 Vbox 的建议,但没有成功,我继续在编译时遇到 Load 异常:

Ive tried suggestions in changing fxroot to a Vbox with no luck, i continue to get a Load exception on the compile :

应用程序启动方法中的异常...引起:javafx.fxml.LoadException:尚未设置根.在加载前使用方法 setRoot().

Exception in Application start method... Caused by: javafx.fxml.LoadException: Root hasn't been set. Use method setRoot() before load.

然而当我使用

scene.setRoot(root); 

遇到同样的异常

我已将问题缩小到我的 FXML 文件无法在 Parent 对象中设置为根的事实,但没有成功解决这个问题.任何建议都会非常感谢.

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 文件指定一个动态根";这意味着 FXML 文件的根是一个在加载文件之前在加载器上设置的对象.这通常用于自定义控件,您希望控件是 Node 的子类,可以使用常规 Java 代码实例化,但希望使用 FXML 定义其布局.<fx:root> 的正确使用(或至少是如何使用它的示例)显示在 标准文档.特别是,如果您使用 ,您必须:

<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:

  1. 创建一个 FXMLLoader 实例,而不是使用静态方便的 FXMLLoader.load(URL) 方法
  2. 对该实例调用 setRoot(...),并传入将成为 FXML 根的对象.
  1. Create an FXMLLoader instance, instead of using the static convenience FXMLLoader.load(URL) method
  2. Call setRoot(...) on that instance, and pass in the object that is to be the root of the FXML.

对于标准 FXML 使用,您只需使用常规实例声明作为根.几乎所有可用的示例都以这种方式工作:可能最好的起点是 官方教程.在你的情况下,因为你想要一个 VBox,你可能只需要

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

<VBox xmlns="javafx.com/javafx/8"; xmlns:fx="javafx.com/fxml/1"; fx:controller="divergex.DivergexGUIController">
<!-- ... -->
</VBox>

编辑如果 Netbeans 给您带来问题,我建议将 Eclipse 与 e(fx)clipse 插件.有一个非常准系统,但几乎所有你需要的,教程.

Edit If 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.

这篇关于设置 root 的 JavaFx FXML 加载文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆