繁体   English   中英

JavaFX Scene Builder控制器

[英]JavaFX Scene builder controller

我正在尝试操纵Scene Builder生成的TextField中的文本。 我的控制器如下所示:

@FXML
private TextField textDescr;

public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
    textDescr = new TextField();
    assert textDescr != null : "fx:id=\"textDescr\" was not injected: check your FXML   file 'provingGroundsUI.fxml'.";
    Game.mainFSM.enter();
}
public void setText(String s) {
    // TODO Auto-generated method stub
    textDescr.setText(s);
}

我收到了NullPointerException。 我尝试了有和没有textDescr = new TextField(); bot textDescr = new TextField(); 部分。 我不太了解...。我以为JavaFX在程序开始时初始化了所有UI变量。

您的控制器类应实现Initializable

@FXML注释显示该字段将由JavaFX初始化。 因此,请确保您删除了new TextField

您确定在FXML中分配了此控制器吗?

  1. 您的FXML外观如何?
  2. 在setText函数中操作textDescr具有很多风险。 最好使用绑定的StringProperty:


    @FXML
    private Text textDescr;

    private StringProperty textProperty = new SimpleStringProperty();  

    @FXML
    void initialize() {
            assert textDescr != null : "fx:id=\"textDescr\" was not injected: check your FXML file 'TestView.fxml'.";
           textDescr.textProperty().bind(textProperty);
    }

    public ReadOnlyStringProperty textProperty(){
          return textProperty;
    }

暂无
暂无

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

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