Design Issue: MVC model with Java CardLayout application

你说的曾经没有我的故事 提交于 2020-01-05 08:34:05

问题


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 with a JPanel ("MainPanel") added as a component and the MainPanel has a CardLayout structure. The MainPanel then has several JPanel's that are flipped through using buttons, Jcombo Box and List selections.

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?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/6811621/design-issue-mvc-model-with-java-cardlayout-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!