Javafx嵌套控制器 [英] Javafx Nested Controller

查看:92
本文介绍了Javafx嵌套控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经一遍又一遍地尝试了这种嵌套控制器的东西,但是它对我不起作用. 我不知道为什么我不能像现在这样简单.我按照这个例子

I have tried this nested controller stuff over and over again, but it just doesn't work for me. I don't know why i can't get something as easy as this to work. I follow this example

<VBox fx:controller="com.foo.MainController">
  <fx:include fx:id="dialog" source="dialog.fxml"/>
  ...
</VBox>

public class MainController extends Controller {
  @FXML private Window dialog;
  @FXML private DialogController dialogController;

  ...
}

这是我的代码: app.Main.fxml

here is my code: app.Main.fxml

<AnchorPane prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="app.MainController">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
        <fx:include source="InnerFile.fxml" fx:id="innerfile"/>
    </children>
</AnchorPane>

app.MainController.java

app.MainController.java

public class MainController {


    @FXML
    private Label label;
    @FXML
    private Button button;
    @FXML
    private InnerFileController controller;


    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");


    }


    public void initialize() {
        controller.here(); 
    }


}

我正在调用嵌套控制器的方法controller.here(); ', 并获得一个NullPointerExecption. 我不知道我做错了什么.

i'm calling a method of the nested controller ' controller.here(); ', and get a NullPointerExecption. I don't know what I have done wrong.

推荐答案

InnerFileController的变量名称不正确.你有:

The name of your variable for InnerFileController is incorrect. You have:

@FXML私有InnerFileController控制器;

@FXML private InnerFileController controller;

但应为:

@FXML私有InnerFileController innerfileController;

@FXML private InnerFileController innerfileController;

这是因为包含文件的控制器变量的名称始终是fx:id值,并在上面加上了"Controller".

This is because the name of the variable for the controller of an included file is always the fx:id value with "Controller" added on to it.

这篇关于Javafx嵌套控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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