设计问题:带有Java CardLayout应用程序的MVC模型 [英] Design Issue: MVC model with Java CardLayout application

查看:97
本文介绍了设计问题:带有Java CardLayout应用程序的MVC模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,标题模糊,我正在尝试布局一个应用程序,很不幸,我没有遵循正确的MVC架构规则就开始编写代码.我有一个具有以下结构的应用程序.

Sorry for the vague title, I am trying to layout an application i have unfortunately starting coding without following proper MVC architecture rules. I have an application that has the following structure.

JFrame,其中添加了JPanel("MainPanel")作为组件,并且MainPanel具有CardLayout结构.然后,MainPanel具有几个JPanel,可以使用按钮,Jcombo Box和List选择进行翻转.

我想使用这种结构来实现MVC模型,但是我很难找到一种方法来实现这一目标.到目前为止,我将所有的actionListener都放在其中,在其中创建了GUI组件,并希望将其分离出来,并为添加到MainPanel的每个JPanel设置一个控制器.我可以通过哪些方法来设计程序,以使我的动作逻辑和数据访问分开,并且仍然保持CardLayout结构?

I want to implement the MVC model using this structure and i am having difficulty finding a way to accomplish this. So far i have all my actionListener's inside where i created the GUI components and want to separate that out and have a controller for each JPanel added to the MainPanel. What are some ways i could design this program in a way that i can seperate my action logic and data accessing and still maintain a CardLayout structure?

推荐答案

动作 是一种便捷的方法封装组件的功能以在菜单,按钮和其他控件中重复使用.您尚未提供有关应用程序模型的详细信息,但可以考虑使用状态模式简化导航模型.此答案可能也提供了一些见识.

As noted in A Swing Architecture Overview, the Swing separable model architecture means that an application may have many models. Buttons and tables are common examples. Actions are a convenient way to encapsulate the functionality of components for reuse in menus, buttons and other controls. You haven't given much detail about your application's model(s), but you might look at using the state pattern to simplify your navigation model. This answer may also offer some insight.

附录:作为一个具体示例, DefaultComboBoxModel .通过扩展该模型,您的视图可以在用户更改组合设置时将自己注册为侦听器并进行适当配置.例如,相关视图也要侦听的程序数据模型所需的值可能会突出显示以表示必填字段.否则可能会禁用或隐藏对当前状态没有意义的控件.

Addendum: As a concrete example, the navigation model used by CardPanel is entirely implicit in the combo's DefaultComboBoxModel. By extending that model, your views may register themselves as listeners and configure themselves appropriately when the user changes the combo setting. For example, a value needed by your program's data model, to which the relevant view also listens, may be highlighted to signify a required field; or a control that makes no sense for the current state may be disabled or hidden.

这篇关于设计问题:带有Java CardLayout应用程序的MVC模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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