java 带有场景生成器的 JavaFX 中的 MVC 模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10872444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC Pattern in JavaFX With Scene Builder
提问by IAE
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.
我是 JavaFX 的新手,鉴于我当前的设置,我正在努力创建合适的 MVC 架构。我使用 Scene Builder 点击了一个 UI 并指定了一个 Controller 类。
Startup:
启动:
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();
}
}
And the Controller class contains the rest of the code.
Controller 类包含其余的代码。
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.
我的教授要求我进一步分离这个应用程序的关注点和责任。Controller 不仅管理状态并与后端通信,还更新 View。
My first response was to let the Controller class become the View and create two other classes for the Controller and Model.
我的第一个反应是让 Controller 类成为 View 并为 Controller 和 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.
但是,我不知道如何连接这些部分。例如,我从不需要实例化视图,因此没有可以传递给控制器的视图实例。接下来,我尝试将它们全部设为单例,并简单地让 Controller 在运行时获取它们,但这给了我一个错误。
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();
}
}
How do I properly set-up an MVC pattern using my current configuration? Is a different architecture required?
如何使用当前配置正确设置 MVC 模式?是否需要不同的架构?
回答by Uluk Biy
Your,
-- Viewis 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.
您的
-- View是Stage
JavaFX 平台在启动时提供的主要内容。此阶段具有唯一Scene
(您已创建并设置),而后者又具有父节点内容root
(您的变量)。该root
节点由 FXMLLoader 设置,代表“PortalUI.fxml”文件中定义的布局/节点结构。
换句话说,Stage -> Scene -> PortalUI.fxml(root)
将定义视图部分。
-- Controlleris 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.
-- Controller是实现的类,Initializable
您在 PortalUI.fxml 文件中使用fx:controller=""属性指定了该类。您在那里指定的类(我想是 PortalController)将由 FXMLLoader 创建并调用其initialize()
方法。即Controller会在PortalUI.fxml文件加载时创建,不需要自己创建和初始化。要从 FXMLLoader获取控制器的创建/初始化实例,请查看访问 FXML 控制器类。
-- Modelis the underlying data structure stored and managed by the controller. It can be anything representing the "data". For example, Person, PortalInfo etc. classes.
--模型是控制器存储和管理的底层数据结构。它可以是任何代表“数据”的东西。例如,Person、PortalInfo 等类。