MVC模式JavaFX中随着场景

MVC模式JavaFX中随着场景

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

问题描述

我是新的JavaFX和我在努力创造给予我的当前设置一个合适的MVC架构。我点击使用起来场景生成器的用户界面,并指定一个控制器类。

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();
    }
}

和控制器类包含code的其余部分。

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.
    }
}

我的教授问我进一步分离这个应用程序的问题和责任。该控制器不仅管理国家,并与后端交谈,而且还更新视图。

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.

不过,我在如何将这些片段连接的损失。我从来没有需要实例化视图,所以我可以通过我的控制器,例如没有查看实例。接下来,我试图让他们所有的单身人士,只是让控制器在运行时获取它们,但是这给了我一个错误。

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?

推荐答案

您,结果
- 查看是主舞台通过在启动时JavaFX平台提供。这个阶段拥有国内唯一的场景(您创建和设置),这反过来又父节点的内容(您变量)。这节点由FXMLLoader设置并重新presents在PortalUI.fxml文件中定义的布局/节点结构。结果
换句话说舞台 - >场景 - > PortalUI.fxml(根)将定义视图的一部分。

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.

- 控制器是类,它实现 Initializable 和您在使用的 FX您PortalUI.fxml文件中指定:控制器= 属性。您指定的有类(PortalController我想)将创建并通过调用FXMLLoader其初始化()方法。即控制器将被创建加载PortalUI.fxml文件时,这样你就不会需要创建和自己初始化。从FXMLLoader得到控制器的创建/初始化实例看。

-- 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.

- 型号是由控制器存储和管理的底层数据结构。它可以是任何重新presenting数据。例如,人,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.

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

08-03 19:32