问题
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