MVC 与 javaFX

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23187932/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:14:49  来源:igfitidea点击:

MVC with javaFX

javamodel-view-controllerjavafxjavafx-2

提问by priojewo

I'm struggling with the MVC concept using javaFX. I am building an javaFX application using fxml files.

我正在为使用 javaFX 的 MVC 概念而苦苦挣扎。我正在使用 fxml 文件构建一个 javaFX 应用程序。

Each fxml file has a controller assigned, but I don't think that this controller is one as the MVC pattern states. I think of it like some sort of ViewController, which holds references to fxml objects (buttons, panes etc.).

每个 fxml 文件都分配了一个控制器,但我不认为这个控制器是 MVC 模式所述的控制器。我认为它就像某种 ViewController,它保存对 fxml 对象(按钮、窗格等)的引用。

My problem is: Where exactly is the difference between this "ViewController" and the real "Controller". What object should do what things? Where do I set e.g. actionListeners?

我的问题是:这个“ViewController”和真正的“Controller”之间的区别究竟在哪里。什么对象应该做什么事?我在哪里设置例如 actionListeners?

回答by jewelsea

Thoughts on MVC

对MVC的思考

MVC is a pretty loosely defined pattern which is open to (often somewhat vague) interpretations of what each of the things in MVC stand for (especially the controller). There is a great discussion of the MVC architecturewith respect to GUI toolkits by Martin Fowler.

MVC 是一种定义相当松散的模式,它对 MVC 中的每个事物(尤其是控制器)所代表的含义(通常有些含糊不清)的解释是开放的。Martin Fowler 对关于 GUI 工具包的 MVC 架构进行了很好的讨论

On Design Patterns and FXML

关于设计模式和 FXML

JavaFX core FXML based processing is built to be more of a toolkit rather than a complete development framework. The idea being that other frameworks could be layered on top of JavaFX and FXML and the underlying JavaFX/FXML implementations and the controllers for them would not push any kind of agenda or architectural constraints on the higher level frameworks.

基于 JavaFX 核心 FXML 的处理更像是一个工具包,而不是一个完整的开发框架。其他框架可以在 JavaFX 和 FXML 以及底层 JavaFX/FXML 实现和它们的控制器之上分层的想法不会在更高级别的框架上推动任何类型的议程或架构约束。

As a result, there is a deliberately loose analogy and mapping of core FXML based processing and their controllers to an MVC architecture.

因此,故意将基于 FXML 的处理及其控制器与 MVC 架构进行了松散的类比和映射。

See JavaFX and MVP – a sm?rg?sbord of design patternsfor further discussion.

请参阅JavaFX 和 MVP – 设计模式的 sm?rg?sbord 以进行进一步讨论。

Consider using a higher-level framework

考虑使用更高级别的框架

You might benefit from adopting an "opinionated" JavaFX framework such as afterburner.fx, which utilizes controllers and FXML but provides a bit more of a rigid structured framework. Afterburner.fx follows a Model View Presenter (MVP) model. Though Afterburner.fx provides more functionality and structure than plain core JavaFX+FXML, it does so in a minimal way by adding few extra classes and APIs that you need to learn to use it.

您可能会受益于采用“自以为是的”JavaFX 框架(例如afterburner.fx ),该框架利用控制器和 FXML,但提供了更多刚性结构化框架。Afterburner.fx 遵循Model View Presenter (MVP) 模型。尽管 Afterburner.fx 提供了比普通核心 JavaFX+FXML 更多的功能和结构,但它通过添加一些您需要学习使用它的额外类和 API 来以最小的方式实现这一点。

回答by Sébastien B.

You can try JRebirth with its custom-tailored pattern: wB-CS-Mvc

您可以尝试使用 JRebirth 的定制模式:wB-CS-Mvc

This is a 2-level MVC pattern, first MVC (application-level) is CSM and the second one is Mvc itself (ui-level).

这是一个 2 级 MVC 模式,第一个 MVC(应用程序级)是 CSM,第二个是 Mvc 本身(ui 级)。

  • Wave for messaging and async internal event handling
  • Behavior to add common code to any Component (C|S|M)
  • Command to manage application commands (reusable code)
  • Service to handle some long service into thread pool
  • Model to manage the Model of your UI
  • View to create your UI
  • Controllers to handle user events
  • 用于消息传递和异步内部事件处理的 Wave
  • 向任何组件添加公共代码的行为 (C|S|M)
  • 管理应用程序命令的命令(可重用代码)
  • 将一些长服务处理到线程池中的服务
  • 用于管理 UI 模型的模型
  • 查看以创建您的 UI
  • 处理用户事件的控制器

You can run the JRebirth Tour Application to learn more (Java 8 required):

您可以运行 JRebirth Tour 应用程序以了解更多信息(需要 Java 8):

http://apps.jrebirth.org/jaf/3.0.0/JRebirthTour-3.0.0.jar

http://apps.jrebirth.org/jaf/3.0.0/JRebirthTour-3.0.0.jar

java -jar JRebirthTour-3.0.0.jar

You can also run the Demo application:

您还可以运行演示应用程序:

http://apps.jrebirth.org/demo/8.5.0/JRebirthDemo-8.5.0.jar

http://apps.jrebirth.org/demo/8.5.0/JRebirthDemo-8.5.0.jar

java -jar JRebirthDemo-8.5.0.jar

or browse slides (slightly outdated): http://fr.slideshare.net/SbastienBordes/javafx-unleashed-with-jrebirth-application-framework

或浏览幻灯片(稍微过时):http: //fr.slideshare.net/SbastienBordes/javafx-unleashed-with-jrebirth-application-framework