ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – MVC模式和Swing

2019-09-15 14:02:09  阅读:168  来源: 互联网

标签:java model-view-controller swing


我发现最难以真正掌握“真正的Swing生活”的设计模式之一是MVC模式.我已经浏览了这个讨论模式的网站上的一些帖子,但我仍然觉得我没有清楚地了解如何利用Java Swing应用程序中的模式.

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

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

我真的很想听听别人对此的看法.当你需要使用MVC模式向用户显示表格,几个字段和一些按钮时,你如何去做?

解决方法:

我强烈推荐给你的摇摆MVC的书是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(如果你想知道什么是“奶油控制器”,想想一个奥利奥饼干,控制器是奶油中心,视图是顶部饼干,模型是底部饼干.)

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

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

编辑#1:

此外,将MVC视为各种模式的复合是很重要的.例如,您的模型可以使用Observer模式实现(需要将View注册为模型的观察者),而您的控制器可能使用策略模式.

编辑#2:

我还想特别回答你的问题.您应该在View中显示表按钮等,这显然会实现ActionListener.在actionPerformed()方法中,您检测事件并将其发送到控制器中的相关方法(请记住 – 视图包含对控制器的引用).因此,当单击按钮时,视图检测到事件,发送到控制器的方法,控制器可能会直接要求视图禁用按钮等.接下来,控制器将与模型交互并修改模型(其中大部分将具有getter和setter方法,以及一些其他方法来注册和通知观察者等等).一旦模型被修改,它将调用已注册观察者的更新(这将是您的情况下的视图).因此,视图现在将自行更新.

标签:java,model-view-controller,swing
来源: https://codeday.me/bug/20190915/1804937.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有