【问题标题】:Why member is not injected in JavaFX controller?为什么成员没有注入 JavaFX 控制器?
【发布时间】:2013-12-05 03:13:08
【问题描述】:

我有以下FXML

<BorderPane fx:controller="mypackage.MainStage" id="BorderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
  <bottom>
    <ToolBar>
      <items>
        <Slider id="slider" prefWidth="443.0" />
        <Label text="Record:" />
        <TextField prefWidth="46.0" />
        <Label text=" of " />
        <Label id="totalLabel" text="0" />
      </items>
    </ToolBar>
  </bottom>
  <center>
    <Pane id="mainPane" prefHeight="200.0" prefWidth="200.0" />
  </center>
  <top>
    <ToolBar>
      <items>
        <Button mnemonicParsing="false" text="Load more" />
      </items>
    </ToolBar>
  </top>
</BorderPane>

以及以下控制器代码:

public class MainStage implements Initializable {

    @FXML
    private Pane mainPane;

    @FXML
    private Label totalLabel;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        totalLabel.setText("245");
    }

}

以及下面的应用代码

public class MyClass extends Application
{

    @Override
    public void start(Stage stage) throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("MainStage.fxml"));

        Scene scene = new Scene(root);

        stage.setTitle("MyClass");
        stage.setScene(scene);
        stage.show();


    }

    public static void main( String[] args )
    {
        launch(args);
    }


}

如果我将断点设置为initialize(),我看到它被调用,但成员是null

为什么不注入?

【问题讨论】:

    标签: java dependency-injection javafx fxml


    【解决方案1】:

    因为我使用了id,而使用fx:id标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 2015-12-27
      • 2012-12-29
      • 1970-01-01
      • 2014-05-23
      • 2013-12-29
      • 1970-01-01
      相关资源
      最近更新 更多