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

查看:1611
本文介绍了如何创建具有不同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:

推荐答案

使用FXML作为组件,使用自定义java类作为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中导入组件时如何实现组件),并且有助于重用代码,就像实现一个自定义的widget 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天全站免登陆