使用JavaFX和FXML的MVC [英] MVC with JavaFX and FXML

查看:245
本文介绍了使用JavaFX和FXML的MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个使用FXML的JavaFX项目,我如何构建它以遵守模型 - 视图 - 控制器模式?这就是我假设一般结构如下:

If I have a JavaFX project that uses FXML, how would I structure it to adhere to the Model-View-Controller pattern? This is what I would assume the general structure to be like:

模型 - 底层程序(GUI代表的内容)。

查看 - FXML文件。

控制器 - FXML控制器。

Model - Underlying program (what the GUI is representing).
View - FXML file.
Controller - FXML controller.

这种表示的问题是视图无法通知模型的变化,因为它只是一个FXML文件。如果视图是FXML控制器类,那么我应该有一个主控制器类,以便FXML控制器从模型中获取信息,主控制器处理动作事件吗?在这种情况下,主控制器将如何访问JavaFX节点?

The issue with this representation is that the view cannot be notified of the model's changes, as it is simply an FXML file. Should the view be the FXML controller class, and then should I have a main controller class such that the FXML controller gets information from the model, and the main controller handles the action events? In this case, how would the main controller access the JavaFX nodes?

此外,在MVC模式中,main方法应该在哪里?现在,它在我的JavaFX Application类中,然后对该程序的其余部分不执行任何操作。另外,JavaFX Application类应该是MVC模式的一部分,还是只需要初始化GUI?

Also, in an MVC pattern, where should the main method be? Right now, It is in my JavaFX Application class, which then does nothing for the remainder of the program. Also, should the JavaFX Application class be part of the MVC pattern, or is it only necessary to initialize the GUI?

问题摘要(请完整阅读虽然发布):


  1. 如何构建使用FXML遵守MVC模式的JavaFX项目?如果视图是FXML控制器类,那么我应该有一个主控制器类,以便FXML控制器从模型中获取信息,主控制器处理动作事件吗?

  2. In一个MVC模式,main方法应该在哪里?

  3. JavaFX Application类应该是MVC模式的一部分,还是只需要初始化GUI?


推荐答案


如何构建使用FXML遵循$ b的JavaFX项目$ b MVC模式?如果视图是FXML控制器类,然后
我应该有一个主控制器类,以便FXML控制器
从模型中获取信息,主控制器处理
动作事件?

How do I structure my JavaFX project which uses FXML to adhere to the MVC pattern? Should the view be the FXML controller class, and then should I have a main controller class such that the FXML controller gets information from the model, and the main controller handles the action events?

JavaFX和Swing都推动了View和Controller之间的紧密耦合,理由是制作通用控制器很困难。然而,MVC本身有一些解释,所以它有点模糊。

Both JavaFX and Swing push towards tight coupling between the View and the Controller, with the justification that making a generic Controller is difficult. However, MVC itself has a few interpretations so it's all a bit vague.

鉴于视图和控制器紧密耦合的这种理由,你有一些选择。我会建议两个,也许其他人可以添加甚至不同意。

Given this justification on the tight coupling of the View and Controller, you have some options. I'll suggest two and maybe others can add or even disagree.

1)接受View类只是一个虚拟加载FXML文件,设置场景舞台所以让 FXMLController 类观察模型和更新视图。

1) Accept that the View class is just a dummy for loading the FXML file, setting the Scene and Stage and so making the FXMLController class observe the model and update the view.

这将Controller限制为JavaFXML应用程序,这意味着它不能轻易地与Swing互换。虽然只要引用正确的Controller并且所有onAction或等效的Node属性都使用相同的引用,但更改View是快速而简单的。

This limits the Controller to JavaFXML applications, meaning it can't easily be interchanged with Swing for instance. Although changing the View is quick and simple as long as it refers to the correct Controller and all onAction or equivalent Node attributes use the same reference.

2)不要使用提供的框架代码。而是使View加载FXML文件并手动访问节点,另外从FXML文件中删除 fx:controller 属性并 onAction 来自FXML文件中相关节点的属性。

2) Don't use the skeleton code provided. Instead make the View load the FXML file and access Nodes manually, additionally removing the fx:controller attribute from your FXML file and onAction attributes from the relevant Nodes in the FXML file.

然后,您可以根据需要访问任何节点视图中的FXML,例如

You can then access any Node as needed from the FXML in the View, e.g.

Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Button example = (Button) root.lookup("#button"); // #button exists in FXMLDocument.fxml
example.setOnAction(e -> { myController.doSomething(); });

这为您提供了优势或指定了您自己的通用控制器,并允许View类观察模型

This gives you the advantage or specifying your own generic controller, and allows the View class to observe the model itself.

在一天结束时,最重要的是从模型和控制器中分离模型,因为模型是程序的核心。
智能模型,瘦控制器,哑视图

At the end of day, the most important thing is to seperate the Model from the View and Controller, since the Model is meat of the program. Smart Model, Thin Controller, Dumb View


主要方法应该在哪里?

Where should the main method be?

无所谓。但是,如果JavaFX Packager Tool创建的JAR是由JavaFX打包器工具完成的,那么JavaFX应用程序并不严格需要 main 方法,而是使用 launch 作为切入点。但是,您可以使用 main 方法作为单独类中的入口点,并调用 Application.launch(MyJavaFXApplicationClass.class); 而不是。

Doesn't matter. However, a JavaFX application doesn't strictly need a main method if the JAR created is done so by the JavaFX Packager Tool, and it instead uses launch as the entry point. You could however use the main method as an entry point in a separate class and call Application.launch(MyJavaFXApplicationClass.class); instead.


JavaFX Application类应该是MVC模式的一部分,还是仅仅
初始化GUI需要什么?

Should the JavaFX Application class be part of the MVC pattern, or is it only necessary to initialize the GUI?

是的,因为它是View的一部分。看看我上面提到的两种方法。

Yes, as it is part of the View. See the two methods I mentioned above.

这篇关于使用JavaFX和FXML的MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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