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

查看:1010
本文介绍了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。所以,有没有办法改变分配的控制器与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.

推荐答案

您可以移除文件中的分配,并通过 FXMLLoader 分配控制器

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天全站免登陆