简体   繁体   English

JavaFX:何时/在何处实例化元素/控件?

[英]JavaFX : When/Where are elements/controls instantiated?

All right, I have my first JFX App Up & Running. 好吧,我有我的第一个JFX App Up&Running。

Now, I am asking myself how everything is nicely wrapped-up behind the scene. 现在,我要问自己,如何将所有事情完美地包裹在幕后。 How is all this bunch of FXML transformed into java code with proper instantiation and all ? 如何通过适当的实例化和全部将这一系列FXML转换为Java代码? How does it work ? 它是如何工作的 ?

I'd Google that if I knew what to write but so far I find a lot about how to use it but not much about how it is made. 我曾向Google询问,如果我知道该写些什么,但到目前为止,我对如何使用它有很多了解,但对如何制造却了解不多。

The magic happens inside the FXMLLoader . 魔术发生在FXMLLoader内部。

It receives an FXML file, and parses it with an XML parser. 它接收一个FXML文件,并使用XML解析器对其进行解析。 You are forced to use a well-defined set of XML nodes, and the loader knows how to "transform" every type of these XML nodes to the corresponding java object (to a JavaFX Node ) at runtime. 您不得不使用一组定义良好的XML节点,并且加载程序知道如何在运行时将这些XML节点的每种类型“转换”为相应的java对象(转换为JavaFX Node )。 If the format of the XML file is not correct (does not fit the defined structure) you will get an exception. 如果XML文件的格式不正确(不适合定义的结构),则会出现异常。 This way your nodes are created, one thing left: populate the controller. 这样,您的节点被创建,剩下的一件事是:填充控制器。

To populate the controller, it will use reflection to create an instance of the specified controller class and to set its data members. 要填充控制器,它将使用反射创建指定控制器类的实例并设置其数据成员。 It iterates through the @FXML annoted fields inside and then set each of them to one of the created objects with the matching fx:id attribute. 它遍历内部@FXML注释的字段,然后将每个字段设置为具有匹配的fx:id属性的已创建对象之一。

In the end by default the public initialize method is called on the controller instance. 最后,默认情况下,在控制器实例上调用public initialize方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM