简体   繁体   English

JavaFX:fx:id校对

[英]JavaFX : fx:id proofing

Entering fx:id 's manually across controllers and FXML files and late binding are a potential source of errors, fragilising code robustness (my personal opinion). 在控制器和FXML文件之间手动输入fx:id以及后期绑定是错误的潜在来源,这使代码健壮性微弱(我个人观点)。 On the other hand, it offers flexibility. 另一方面,它提供了灵活性。

Is there a way of proofing the binding and identifying errors before revealing them at runtime, or is this the "by design" workflow and you have to make do and just be careful while coding ? 有没有一种方法可以在运行时显示绑定之前证明绑定和识别错误,或者这是“设计使然”的工作流程,您必须这样做并且在编码时要小心一点?

edit : 编辑:
I am using Eclipse with e(fx)clipse, and Gluon Scene Builder 我正在使用带有e(fx)clipse和Gluon Scene Builder的Eclipse

As far as I know there is no real "proof" like the javac which would complain. 据我所知,没有像javac这样会抱怨的真正“证明”。 JavaFx to too dynmaic for this. JavaFx对此不太动态。 The best thing you can do is adding the controller to your FXML file like this 最好的办法是像这样将控制器添加到FXML文件中

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<ScrollPane xmlns="http://javafx.com/javafx/8" 
    xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.myapp.MyController">
    <content>
        ...
    </content>
</ScrollPane>

This then allows Eclipse or IntelliJ (thx @silifly) to show you warning about elements which have not been binded to you controller. 然后,这允许EclipseIntelliJ (thx @silifly)向您显示有关尚未绑定到控制器的元素的警告。 Bu this will not tell if the injection which is performed by the FXMLLoader was successful. 但是这不会告诉您由FXMLLoader执行的FXMLLoader是否成功。 You then could check during the initialization phase if your element has been injected correctly. 然后,您可以在初始化阶段检查元素是否已正确注入。

import javafx.fxml.FXML;
import javafx.fxml.Initializable;

public class MyController implements Initializable {

    @FXML
    private Button btnLogin;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        assert btnLogin != null : "fx:id=\"btnLogin\" was not injected: check your FXML file 'MyView.fxml'.";
    }
}

Those are the two facilities which I know and use a lot to check if everything has been setup correctly. 这些是我所知道的两个设施,并且经常使用它们来检查所有设置是否正确。

Edit: 编辑:

I just saw that you don't need to implement the interface anymore you can just write 我只是看到您不再需要实现接口,您只需编写即可

@FXML
public void initialize() {
    ...
}

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

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