javafx中的initialize()是什么意思? [英] what does initialize() mean in javafx?

查看:1093
本文介绍了javafx中的initialize()是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中写了一些代码,发现应该将buttonlistener放在public void initialize()中,否则我将无法使用该按钮.我不知道javafx中的initialize()是什么意思,为什么不能将其更改为另一个方法名称?

I wrote some code in my project, and I found that I should put my buttonlistener in the public void initialize() , or I can't use the button. I don't know what the initialize() mean in javafx , why I can't change it to another method name?

这是我代码中的initialize方法,我没有实现Initializable.那么在javafx中这不是什么意思?

This is initialize method in my code, I don't implement Initializable. So what't the mean of this in javafx?

public void initialize() {
    weight_number1.setOnAction(e -> number(1));
    weight_number2.setOnAction(e -> number(2));
    weight_number3.setOnAction(e -> number(3));
    weight_number4.setOnAction(e -> number(4));
    weight_number5.setOnAction(e -> number(5));
    weight_number6.setOnAction(e -> number(6));
    weight_number7.setOnAction(e -> number(7));
    weight_number8.setOnAction(e -> number(8));
    weight_number9.setOnAction(e -> number(9));
    weight_number0.setOnAction(e -> number(0));
    weight_numberCE.setOnAction(e -> symbol(1));
    weight_numberLeft.setOnAction(e -> symbol(2));
    weight_numberp.setOnAction(e -> symbol(3));

    weight_box1.setOnAction((event) -> {
        change();
        showans();
    });
    weight_box2.setOnAction((event) -> {
        change();
        showans();
    });
}

推荐答案

initialize()方法用于初始化任何控件,尤其是当它不能通过FXML完成时.例如setCellFactory()setCellValueFactory().如果您没有要初始化的内容,则无需在控制器类中放置initialize()方法.

The initialize() method is used to initialize any controls, especially when it is something that can't be done from the FXML. An example of this is setCellFactory() and setCellValueFactory(). If you have nothing to initialize, then you do not need to put an initialize() method in your controller class.

有人认为他们可以在控制器类的构造函数中进行初始化,但这是错误的初始化位置.这是因为FXML注入仅在实例化控制器类之后发生. FXML注入恰好在调用initialize()之前发生,这使得在initialize()中执行初始化安全.如果您使用注入控件的引用(例如@FXML private Button button),则在构造函数中进行初始化将为您提供NullPointerException.

Some people thought that they could initialize in the controller class constructor, but that is the wrong place to initialize. This is because FXML injection only occurs after the controller class has been instantiated. FXML injection occurs just before initialize() is being called, which makes it safe to perform initialization in initialize(). Initializing in constructor will give you NullPointerException, if you used the reference of an injected control (e.g. @FXML private Button button).

在JavaFX 2.0中,如果控制器有任何要初始化的内容,则需要控制器类来实现Initializable接口.如果添加initialize()而不实现该接口,则不会进行初始化.

In JavaFX 2.0, controller classes were required to implement the Initializable interface if the controller has anything to initialize. If you added initialize() without implementing the interface, no initialization will occur.

这在JavaFX 8.0中已更改.控制器类将不再需要实现Initializable接口,但是您可以仍然选择这样做.唯一需要注意的是,如果initialize()是非公开的,则需要添加@FXML批注.如果initialize()方法是public,那么您可以跳过注释.

This was changed in JavaFX 8.0. Controller classes will no longer need to implement Initializable interface, but you may still choose to do so. The only thing to take note is that you need to add the @FXML annotation if the initialize() is non-public. If the initialize() method is public, then you can just skip the annotation.

我建议保留initialize()方法private(或protected,这很少有用).保持initialize() private会使不太可能进行多次初始化,这有可能导致意外行为.

I would recommend keeping the initialize() method private (or protected, which is rarely useful). Keeping initialize() private makes it less likely for initialization to occur more than once, which could potentially cause unexpected behavior.

如果您想知道initialize()在未实现Initializable接口时如何被调用,则可以通过使用Java Reflection来调用它.这就是为什么您需要使用确切的方法名称的原因.

In case you are wondering how initialize() gets called when it does not implement the Initializable interface, it is called via the use of Java Reflection. That is why you need to use the exact method name.

这篇关于javafx中的initialize()是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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