如何使用不同的 fxml 文件创建多个 javafx 控制器? [英] How to create multiple javafx controllers with different fxml files?

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

问题描述

我一直在查看一些博客和其他 stackoverflow 问题,但没有看到我的问题的直接答案.我正在创建一个 javafx gui 客户端,我想让我的菜单栏成为一个 fxml 中的一个控制器,然后我想让内容区域成为额外的 fxml 文件.登录屏幕将是一个 fxml,在登录屏幕之后将是应用程序的主要内容,这将在一个 fxml 中.我该怎么做?

I've been looking at some blogs and other stackoverflow questions, and I'm not seeing a direct answer to my question. I am creating a javafx gui client and I want to have my menubar be one controller in one fxml and then i want the content area to be additional fxml files. The login screen will be one fxml, after the login screen will be the main content of the application and that will be in one fxml. How do i go about doing this?

我只是不想将我的登录、菜单栏和主要内容的所有代码都放在同一个文件中.这是我正在处理的图像:

I just don't want to have all of my code for my login, menubar, and main content in the same file. This is an image of what i am working on:

推荐答案

通过使用自定义 Java 类作为 FXML 文件的 fx:root 和 fx:controller,将 FXML 用作组件:http://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm

Use FXML as components by using a custom java class as fx:root and as fx:controller of your FXML file: http://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm

为此,您需要调用自定义 Java 类 FXMLLoader 的构造函数,它将加载您的 FXML.好处是改变了FXML加载组件的方式.

To do so, you need to call in the constructor of your custom java class FXMLLoader which will load your FXML. The advantage is to change the way FXML load components.

通过带有嵌套控制器的 FXMLLoader 实例化组件的经典方法是:首先 FXML,然后是每个部分的控制器.

The classic way to instanciate components via FXMLLoader with nested controllers is: FXML first, then controller for each part.

使用这种技术是:首先是控制器,然后是每个组件的 FXML.并且您不会直接在 FXML 中加载 FXML,您将在 FXML 中导入您的自定义 java 类.

With this technique this is: controller first, then FXML for each component. And you won't load FXML in FXML directly, you will import your custom java classes in the FXML.

这是一个更好的抽象(在 FXML 中导入组件时无需知道组件是如何实现的)并且有助于重用代码,就像实现具有 FXML 支持的自定义小部件一样.为了使您的组件可重用,请确保您的实现与其他部分没有紧密耦合,或者使用 IOC 来做到这一点(例如,使用 Spring 与 JavaFX 的集成).这样,您就可以在应用程序的任何部分(就像 DateInput 小部件一样)导入您的组件而无需担心,并且不会重复代码.

This is a better abstraction (no need to know how a component is implemented when you import them in FXML) and helps reusing code as it is like implementing a custom widget with FXML support. To make your component reusable, make sure your implementation doesn't have tight coupling with other parts, or use IOC to do so (for instance, with Spring integration with JavaFX). This way, you will be able to import your component in any part of your application (just like a DateInput widget) without worry and you won't duplicate code.

就您而言,您将拥有:

public class MenuBox extends VBox {

@FXML
private LoginBox loginBox;

@FXML
private ProfilesBox profilesBox;

public MenuBox() {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("menu.fxml"));
    fxmlLoader.setRoot(this);
    fxmlLoader.setController(this);
    try {
        fxmlLoader.load();
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    }
}

public class LoginBox extends VBox {
public LoginBox() {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("login.fxml"));
    fxmlLoader.setRoot(this);
    fxmlLoader.setController(this);
    try {
        fxmlLoader.load();
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    }
}

public class ProfilesBox extends VBox {
public ProfilesBox() {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("profiles.fxml"));
    fxmlLoader.setRoot(this);
    fxmlLoader.setController(this);
    try {
        fxmlLoader.load();
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    }
}

您将在管理页面全局布局的 menu.fxml 中导入 LoginBox 和 ProfilesBox:

And you will import LoginBox and ProfilesBox in menu.fxml that manages the global layout for your page:

<?import com.foo.bar.LoginBox ?>
<?import com.foo.bar.ProfilesBox ?>
<fx:root type="javafx.scene.layout.VBox"
    xmlns:fx="http://javafx.com/fxml">

<!-- Stuff here to declare the menu bar-->

    <HBox>
       <ProfilesBox fx:id="profilesBox"/>
       <LoginBox fx:id="loginBox"/>
    </HBox>

</fx:root>

login.fxml 和profiles.fxml 只包含基本组件.

login.fxml and profiles.fxml contain just basic components.

这篇关于如何使用不同的 fxml 文件创建多个 javafx 控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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