每个声明都需要@FXML吗? [英] Is @FXML needed for every declaration?

查看:176
本文介绍了每个声明都需要@FXML吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个声明都需要@FXML,还是第一个?

Is @FXML needed for every declaration or just for the first?

换句话说,我应该使用

@FXML
public Label timerLabel = new Label();
@FXML
public TextField mainTextField, projectTextField ;
@FXML
public Button goButton, deleteAllButton ;
@FXML
public ComboBox<String> projectComboBox ;
@FXML
public TableView<Entry> mainTable ;
@FXML
public TableColumn<Entry, String> titleColumn, timeColumn, dateColumn ;
@FXML
public TableColumn<Entry, Boolean> checkColumn, buttonColumn ;
@FXML
public checkBox checkAllCheckBox ;

@FXML
public Label timerLabel = new Label();
public TextField mainTextField, projectTextField ;
public Button goButton, deleteAllButton ;
public ComboBox<String> projectComboBox ;
public TableView<Entry> mainTable ;
public TableColumn<Entry, String> titleColumn, timeColumn, dateColumn ;
public TableColumn<Entry, Boolean> checkColumn, buttonColumn ;
public checkBox checkAllCheckBox ;

谢谢!

推荐答案

@FXML 注释启用 FXMLLoader 将FXML文件中定义的值注入引用在控制器类中。换句话说,如果您使用 @FXML 注释 timerLabel ,那么它将由<$ c $初始化c> FXMLLoader 当FXML文件中的元素使用 fx:id =调用 load()方法时timerLabel。正如其他人在评论中指出的那样,这意味着你永远不应该写代码

The @FXML annotation enables an FXMLLoader to inject values defined in an FXML file into references in the controller class. In other words, if you annotate your timerLabel with @FXML, then it will be initialized by the FXMLLoader when the load() method is called by an element in the FXML file with fx:id="timerLabel". As others have pointed out in the comments, this means you should never write code like

@FXML
private Label timerLabel = new Label();

此处 timerLabel 将首先初始化为您在代码中创建的 new Label(); ,然后几乎立即重新初始化为FXML文件中定义的值。这至多是多余的,最糟糕的是误导。如果您没有将变量名称正确匹配到 fx:id ,则您的变量将引用错误的标签并且错误将很难追踪。

Here timerLabel will first be initialized to the new Label(); you create in the code, and will then almost immediately be re-initialized to the value defined in the FXML file. This is at best redundant, and at worst misleading. If you don't correctly match the variable names to the fx:id, your variable will be referring to the wrong Label and the error will be very difficult to track down.

要找到您的实际问题:

何时 FXMLLoader 加载FXML文件,它会尝试将任何具有 fx:id 属性的元素注入控制器。它将查找

When the FXMLLoader loads the FXML file, it will attempt to inject any elements that have an fx:id attribute into the controller. It will look for


  1. 任何 public 字段,其变量名称与<$匹配c $ c> fx:id 属性,

  2. 任何字段( public 或不具有与 fx:id 属性匹配的变量名称,该属性使用 @FXML 进行注释。

  1. Any public field with a variable name matching the fx:id attribute, or
  2. Any field (public or not) with a variable name matching the fx:id attribute that is annotated with @FXML.

因此在您的示例中,由于您的所有字段都是 public ,因此您可以省略所有字段 @FXML 注释(即使是第一个)也会有效。

So in your example, since all your fields are public, you can omit all the @FXML annotations (even the first) and it will still work.

但是,如果你遵循良好做法并使你的字段 private ,然后每个声明必须注释 @FXML 才能使注入工作。

However, if you follow good practice and make your fields private, then each declaration must be annotated @FXML for the injection to work.

所以

@FXML
private Label timerLabel;
@FXML
private TextField mainTextField;

等可行,但

@FXML
private Label timerLabel;
private TextField mainTextField;

不会。

这篇关于每个声明都需要@FXML吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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