Javafx FXMLLoader类 [英] Javafx FXMLLoader class

查看:353
本文介绍了Javafx FXMLLoader类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Javafx时,我已经研究了阶段,场景和场景图(树数据结构)(分支节点,叶节点)等... 所以我知道场景图的基础知识,它必须包含一个根节点及其子节点,并且场景类采用类型为根节点的参数,所以我的问题是 当我写此行时:

While i am studying Javafx i had already studied the stage , scene and scene graph ( tree data structure )( branch node , leaf node ) etc ... So i know the basics of the scene graph that it must contain a root node and its children and the scene class takes a parameter of type root node so my question is when i write this line :

FXMLLoader load = new FXMLLoader(getClass.getResource("sample.fxml"));

所以我知道我在这里创建FXMLLoader的对象,所以这实际上在做什么?我只是想知道当我使用FXMLLoader加载我的.fxml代码时会发生什么... 是否像没有javafx或CSS的基本方法那样创建没有.fxml的类? 还是此FXMLLoader返回根节点及其子节点. 总之,我想知道这个FXMLLoader的工作原理.

So i know that here i am creating an object of the FXMLLoader so here what is actually going on ? i just wanted to know what happens when i use the FXMLLoader to load my .fxml code ... Does it create a class without .fxml like the basic way without javafx or CSS ? Or does this FXMLLoader returns to root node and its children. In summary i want to know how this FXMLLoader works.

推荐答案

FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));

与任何其他类似的Java代码一样,它创建了FXMLLoader类的实例.您还可以将其location属性设置为指定的URL(基本上在与您所在的类相同的包中表​​示sample.fxml资源).在调用

does just the same as any other similar Java code, it creates an instance of the FXMLLoader class. You also set its location property to the URL you specify (basically representing the sample.fxml resource in the same package as the class you are in). This doesn't load or read the FXML file until you call

loader.load();

当您调用它时,它将读取并解析FXML文件,并创建与FXML中的元素相对应的对象层次结构.如果FXML指定了一个控制器,则它将具有fx:id属性的所有元素注入到该控制器中与该属性同名的带有@FXML注释的字段中.一旦完成,它将调用控制器的initialize()方法(如果有的话),最后返回与FXML文件的根元素相对应的对象.此对象也设置为root属性,因此以下代码相同:

When you do call this, it reads and parses the FXML file, and creates an object hierarchy corresponding to the elements in the FXML. If the FXML specifies a controller, it injects any elements with fx:id attributes into @FXML-annotated fields in the controller with the same name as the the attribute. Once that is complete, it calls the controller's initialize() method, if it has one, and finally returns the object corresponding to the root element of the FXML file. This object is also set as the root property, so the following code is identical:

loader.load();
Parent root = loader.getRoot();

Parent root = loader.load();

作为一个例子,假设您的FXML是

As an example, suppose your FXML were

<BorderPane fx:controller="example.Controller">
  <top>
    <Label fx:id="header"/>
  </top>
  <bottom>
    <HBox>
      <children>
        <Button text="OK" fx:id="okButton" />
        <Button text="Cancel" fx:id="cancelButton" />
      </children>
    </HBox>
  </bottom>
</BorderPane>

然后

Parent root = loader.load();

使要执行的代码与在加载程序中执行以下代码具有完全相同的效果:

causes code to be executed that has the exact same effect as executing the following in the loader:

public class FXMLLoader {

    // not a real method, but functionally equivalent to the load()
    // method for the example FXML above:
    public BorderPane load() {

        example.Controller controller = new example.Controller();
        this.controller = controller ;

        BorderPane borderPane = new BorderPane();
        this.root = borderPane ;

        Label header = new Label();
        controller.header = header ;
        borderPane.setTop(header);

        HBox hbox = new HBox();
        Button okButton = new Button();
        okButton.setText("OK");
        controller.okButton = okButton ;
        hbox.getChildren().add(okButton);

        Button cancelButton = new Button();
        cancelButton.setText("Cancel");
        controller.cancelButton = cancelButton ;
        hbox.getChildren().add(cancelButton);

        borderPane.setBottom(hbox);

        controller.initialize();

        return borderPane ;

    }
}

当然,由于它是在运行时读取FXML文件的,因此实际上所有这些操作都是通过反射完成的,但是代码的效果是相同的.上面的任何实际代码都不存在.

Of course, since it's reading the FXML file at runtime, all this is actually done by reflection, but the effect of the code is the same. At no point does any of the actual code above ever exist.

FXML文档简介提供了FXML文档的完整规范;显然,这里的文章太多了,无法涵盖所有​​内容.

The Introduction to FXML document provides a full specification of the FXML document; obviously there is far too much there to cover everything in a post here.

这篇关于Javafx FXMLLoader类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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