草庐IT

java - MVC 模式和 Swing

coder 2023-04-28 原文

我发现在“真实的 Swing 生活”中最难真正掌握的设计模式之一是 MVC 模式。我已经浏览了该站点上很多讨论该模式的帖子,但我仍然觉得我对如何在我的 Java Swing 应用程序中利用该模式没有清楚的了解。

假设我有一个 JFrame,其中包含一个表格、几个文本字段和几个按钮。我可能会使用 TableModel 将 JTable 与底层数据模型“桥接”起来。但是,所有负责清除字段、验证字段、锁定字段以及按钮操作的功能通常都直接在 JFrame 中。但是,这不是把模式的Controller和View混在一起了吗?

据我所知,在查看 JTable(和模型)时,我设法“正确”实现了 MVC 模式,但是当我查看整个 JFrame 时,事情变得困惑了。

我真的很想听听其他人对此的看法。当您需要使用 MVC 模式向用户显示一个表格、几个字段和一些按钮时,您会怎么做?

最佳答案

我强烈推荐给你的一本关于 MVC in swing 的书是 Freeman 和 Freeman 的“Head First Design Patterns”。他们对 MVC 有非常全面的解释。

Brief Summary

  1. You're the user--you interact with the view. The view is your window to the model. When you do something to the view (like click the Play button) then the view tells the controller what you did. It's the controller's job to handle that.

  2. The controller asks the model to change its state. The controller takes your actions and interprets them. If you click on a button, it's the controller's job to figure out what that means and how the model should be manipulated based on that action.

  3. The controller may also ask the view to change. When the controller receives an action from the view, it may need to tell the view to change as a result. For example, the controller could enable or disable certain buttons or menu items in the interface.

  4. The model notifies the view when its state has changed. When something changes in the model, based either on some action you took (like clicking a button) or some other internal change (like the next song in the playlist has started), the model notifies the view that its state has changed.

  5. The view asks the model for state. The view gets the state it displays directly from the model. For instance, when the model notifies the view that a new song has started playing, the view requests the song name from the model and displays it. The view might also ask the model for state as the result of the controller requesting some change in the view.

Source (如果你想知道什么是“奶油 Controller ”,可以想象一下奥利奥 cookies , Controller 是奶油中心, View 是顶部 cookies ,模型是底部 cookies 。)

嗯,如果您有兴趣,可以从 here 下载一首关于 MVC 模式的相当有趣的歌曲。 !

您在使用 Swing 编程时可能遇到的一个问题涉及将 SwingWorker 和 EventDispatch 线程与 MVC 模式合并。根据您的程序,您的 View 或 Controller 可能必须扩展 SwingWorker 并覆盖放置资源密集型逻辑的 doInBackground() 方法。这可以很容易地与典型的 MVC 模式融合,并且是 Swing 应用程序的典型。

编辑#1:

此外,将 MVC 视为各种模式的组合也很重要。例如,您的模型可以使用观察者模式(需要将 View 注册为模型的观察者)来实现,而您的 Controller 可能使用策略模式。

编辑#2:

我还想具体回答您的问题。您应该在 View 中显示您的表格按钮等,这显然会实现一个 ActionListener。在您的 actionPerformed() 方法中,您检测事件并将其发送到 Controller 中的相关方法(请记住 - View 包含对 Controller 的引用)。所以当一个按钮被点击时, View 检测到事件,发送给 Controller 的方法, Controller 可能会直接要求 View 禁用按钮什么的。接下来, Controller 将与模型进行交互和修改(主要有 getter 和 setter 方法,以及其他一些用于注册和通知观察者的方法等)。一旦模型被修改,它将调用注册观察者的更新(这将是您的情况下的 View )。因此, View 现在将自行更新。

关于java - MVC 模式和 Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5217611/

有关java - MVC 模式和 Swing的更多相关文章

  1. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  4. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  5. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  6. ruby - 是否有用于序列化和反序列化各种格式的对象层次结构的模式? - 2

    给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最

  7. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  8. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

    什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

  9. Observability:从零开始创建 Java 微服务并监控它 (二) - 2

    这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/

  10. 【Java 面试合集】HashMap中为什么引入红黑树,而不是AVL树呢 - 2

    HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候

随机推荐