具有多个不同控制器的 JavaFX 1 FXML 文件? [英] JavaFX 1 FXML File with Multiple Different Controllers?

查看:36
本文介绍了具有多个不同控制器的 JavaFX 1 FXML 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有两个不同的阶段,它们是使用相同 FXML 文件的帮助屏幕.我不想创建 2 个 FXML 文件,而是只使用一个并有两个​​调用相同 fxml 的控制器.

There are two different stages in my application that are help screens that use the same FXML file. Rather than create 2 FXML files, I would like to use just one and have two controllers that call the same fxml.

唯一的问题是控制器是在 FXML 文件中分配的.那么,有没有办法使用 Controller 类本身中的代码更改分配的控制器?

The only problem is that the Controller is assigned in the FXML file. So, is there a way to change the assigned controller with code in the Controller class itself?

我真的很想避免复制 FXML 文件只是为了更改每个文件中的控制器.提前致谢.

I'd really like to avoid duplicating an FXML file just to change the Controller in each. Thanks in advance.

推荐答案

您可以从 FXML 文件中删除 fx:controller="" 分配并分配控制器在加载过程中通过 FXMLLoader.

You can remove the fx:controller="" assignment from the FXML file and assign the controller via the FXMLLoader during the load.

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Your.fxml"));
fxmlLoader.setController(this);

try
{
    fxmlLoader.load();
}
catch (IOException exception)
{
    throw new RuntimeException(exception);
}

查看FXML 简介自定义组件部分.

这篇关于具有多个不同控制器的 JavaFX 1 FXML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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