简体   繁体   English

如何初始化自定义javafx控制器的成员变量?

[英]How to initialize member variables of a custom javafx controller?

In the Spring framework, i can use the configuration files to load member variables of a class. 在Spring框架中,我可以使用配置文件来加载类的成员变量。 Is there a way to do this in javafx with a custom controller or a custom object? 有没有办法在javafx中使用自定义控制器或自定义对象执行此操作?

The @FXML annotation enables the JavaFX objects whose names you defined (fx:id) to have their references reflectively injected into nonpublic fields in the controller object as the scene graph is loaded from the fxml markup. @FXML注释使得您定义的名称(fx:id)的JavaFX对象能够将其引用反射注入控制器对象中的非公共字段,因为场景图是从fxml标记加载的。

You can accomplish something very similar to what you are requesting by defining the values that you want set as class variables in your controller object's class, and then setting the appropriate object properties programmatically (rather than in markup) in the initialize() method of your controller object. 您可以通过在控制器对象的类中定义要设置为类变量的值,然后在您的initialize()方法中以编程方式(而不是在标记中)设置适当的对象属性,从而完成与请求非常相似的操作。控制器对象。

The initialize() method is called (if it is present) after the loading of the scene graph is complete (so all the GUI objects will have been instantiated) but before control has returned to your application's invoking code. 在加载场景图完成后调用initialize()方法(如果它存在)(因此所有GUI对象都将被实例化)但在控制返回到应用程序的调用代码之前。

Edit 编辑

You can use @FXML only in Controller which is specifically set in fxml file and only for fields of that class. 您只能在Controller中使用@FXML,该控制器专门在fxml文件中设置,并且仅适用于该类的字段。

This is required because these fields would be initialized automatically during creation of that class' object. 这是必需的,因为这些字段将在创建该类对象期间自动初始化。

 public class MyController implements Initializable{

      @FXML
      Button startButton;

      void initialize(java.net.URL location, java.util.ResourceBundle resources) {
           startButton.addActionLisetner(...);
      }

 }

Detailed tutorial is here 详细的教程在这里

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

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