带有场景生成器的

带有场景生成器的

本文介绍了带有场景生成器的 JavaFX 中的 MVC 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JavaFX 的新手,鉴于我当前的设置,我正在努力创建合适的 MVC 架构.我使用 Scene Builder 点击了一个 UI 并指定了一个 Controller 类.

I'm new to JavaFX and am struggling to create a proper MVC architecture given my current setup. I clicked together a UI using Scene Builder and designated a Controller class.

启动:

public class Portal extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("PortalUI.fxml"));

        stage.setTitle("Portal");
        stage.setScene(new Scene(root));
        stage.show();
    }
}

Controller 类包含其余的代码.

And the Controller class contains the rest of the code.

public class AccommodationPortalView implements Initializable {
    @Override
    public void initialize(URL url, ResourceBundle resources) {
    // Work here.
    }
}

我的教授要求我进一步分离此应用程序的关注点和责任.Controller 不仅管理状态并与后端通信,还更新 View.

My professor asked that I further separate the concerns and responsibilities of this application. The Controller is not only managing state and talking with the backend, but also updating the View.

我的第一反应是让控制器类成为视图并为控制器和模型创建另外两个类.

My first response was to let the Controller class become the View and create two other classes for the Controller and Model.

但是,我不知道如何连接这些部分.例如,我从不需要实例化视图,因此没有可以传递给控制器​​的视图实例.接下来,我尝试将它们全部设为单例,并简单地让 Controller 在运行时获取它们,但这给了我一个错误.

However, I'm at a loss at how to connect these pieces. I never need to instantiate the View, so there is no View instance that I can pass to my Controller, for example. Next, I tried making them all singletons and simply letting Controller fetch them at runtime, but that gives me an error.

public class Portal extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("PortalUI.fxml"));

        stage.setTitle("Portal");
        stage.setScene(new Scene(root));
        stage.show();

        // Controller gets a View and Model instance in initialize();
        // Error: Instantiation and Runtime Exception...
        PortalController.INSTANCE.initialize();
    }
}

如何使用当前配置正确设置 MVC 模式?是否需要不同的架构?

How do I properly set-up an MVC pattern using my current configuration? Is a different architecture required?

推荐答案

您的,
-- View 是 JavaFX 平台在启动时提供的主要 Stage.这个阶段只有一个 Scene(你已经创建和设置),它又具有一个父节点内容 root(你的变量).此 root 节点由 FXMLLoader 设置,表示PortalUI.fxml"文件中定义的布局/节点结构.
换句话说 Stage ->场景 ->PortalUI.fxml(root) 将定义视图部分.

Your,
-- View is a primary Stage provided by the JavaFX platform at start up. This stage has the only Scene (you have created and set) which in turn has a parent node content root (your variable). This root node is set by FXMLLoader and represents the layout/node structure defined in the "PortalUI.fxml" file.
In other words Stage -> Scene -> PortalUI.fxml(root) will define the view part.

-- Controller 是实现 Initializable 和您在 PortalUI.fxml 文件中使用 fx:controller=" " 指定的类属性.您在那里指定的类(我想是 PortalController)将被 FXMLLoader 创建并调用它的 initialize() 方法.即Controller会在PortalUI.fxml文件加载时创建,不需要自己创建和初始化.要从 FXMLLoader 获取创建/初始化的控制器实例,请查看访问 FXML 控制器类.

-- Controller is the class that implements Initializable and that you specified in your PortalUI.fxml file with fx:controller=" " attribute. The class you have specified there (PortalController I suppose) will be created and invoked its initialize() method by the FXMLLoader. Namely the Controller will be created when the PortalUI.fxml file is loaded, so you don't need to create and initialize it yourself. To get the created/initialized instance of the controller from the FXMLLoader look the Accessing FXML controller class.

-- Model 是控制器存储和管理的底层数据结构.它可以是任何代表数据"的东西.例如,Person、PortalInfo 等类.

-- Model is the underlying data structure stored and managed by the controller. It can be anything representing the "data". For example, Person, PortalInfo etc. classes.

这篇关于带有场景生成器的 JavaFX 中的 MVC 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:32