MVC架构和模态对话框窗口

3
我正在开发一个MVC架构的项目。它应该是一个简单的应用程序,用于管理一些客户。
有MainModel、MainView和MainController类,它们使主窗口显示客户表的内容,并允许用户插入、删除或编辑客户。
我的问题是,插入和编辑按钮应该显示一些对话框窗口,让用户插入和编辑一些文本值,我有些疑问。
我想问你一些问题:
  • Should I use the MVC architecture for each dialog window?
  • If yes, I have tried doing it but my dialog windows are modal, so my code runs the model, runs the view but it gets blocked in the view and it doesn't run the controller class. How could I solve it?

    For example here it gets blocked in the "new InsertCustomerController..." instruction:

        CustomerModel customerModel = new CustomerModel();
        InsertCustomerView insertCustomerView = new insertCustomerView(customerModel);
        new InsertCustomerController(insertCustomerView, customerModel);
    
非常感谢您。

我唯一的想法是定义一个简单的方法,在我的视图中将对话框设置为模态,并在控制器构造函数中调用它,但我不知道这样是否可行。 我甚至不知道在我的对话框窗口中是否应该使用MVC... - JohnQ
1
没有看到更多的代码,很难说清楚发生了什么。你的InsertCustomerView和InsertCustomerController里面有什么内容?尝试发布一个SSCCE。我很难理解你的问题以及具体的问题是什么。 - Guillaume Polet
1
你不一定需要多个控制器。控制器的作用是将事物路由到视图和模型。它实际上是从键盘到视图的桥梁。主控制器可以处理其他项目。另一个选项是颠倒过来。创建一个新的控制器实例,让它创建模型和视图,视图代码将执行你卡住的对话框。 - Lucas Holt
1
只是一点小注:MVC 不是一个架构,而是一种设计模式。 - tereško
2个回答

2
无论使用哪种模式,您都可以使用观察者模式来使对话框与应用程序的模型同步。此示例使用PropertyChangeListener;其他方法在这里提到。

0

虽然我无法完全从您的帖子中确定,但我不确定您是否正确地考虑了MVC。但假设您有一个Customer,CustomerView和CustomerController类..

Customer可以包含与成为客户相关的所有业务逻辑 - 因此它可能具有诸如setBalance(int newBalance),getBalance()等方法。

CustomerView类本质上可以是JPanel或JFrame的子类(因为根据您的问题标签,看起来您正在使用Swing)。此类将代表一个Customer实例。也许您可以有一个私有的Customer类变量。这个类的职责应该仅包括显示其Customer实例中包含的数据以及允许用户修改它们。

由于我不知道您的应用程序,因此很难说CustomerController将要做什么,但它可能会包含ActionListeners等有助于将输入和输出路由到模型和视图的不同部分的内容。

我通过一些谷歌搜索找到了一个非常简单的示例,您可能想查看: http://www.austintek.com/mvc/

祝你好运。希望这有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接