将 MVC/MVP 用于 Swing UI? [英] Using MVC/MVP for Swing UI?
问题描述
到现在为止,我已经使用了一些 java swing 代码,并将所有内容都放在一个类中.我想要设计和开发更好的 UI 应用程序.尝试将 MVP/MVC 用于 Swing UI,在任何地方都没有找到任何具体示例.
Till now I have worked with some java swing code with everything in one class. I want design and develop better UI application. Trying to use MVP/MVC for swing UI, didn't found any concrete example anywhere.
我想开发带有提交按钮的 Swing UI 表单.即这个代表用户表单的主面板包含三个面板:- 带有组合框和文本字段的标题面板.- 带有表格窗格的中心面板.- 带有一些组合框和文本字段的结束面板.
I want to develop swing UI form with submit button. i.e. this main panel representing user form contains three panels: - Header panel with combo box and text fields. - Center panel with table pane. - End panel with some combo box and text fields.
所以基本上我已经编写了 4 个没有动作监听器的 UI 类,一个用于主面板,三个用于标题、中心和结束面板.执行主面板显示所有组件.
So basically I have written 4 UI classes with no action listeners, one for main and three for header,center and end panel. Executing Main panel show me all the components.
我想向组合框提供运行时数据,当用户提交时,我应该从组合框和其他字段中获取选定的值.我应该如何使用 MVP/MVC 进行设计.任何人都可以提供一些解释或给我任何参考或示例的链接吗?
I want to provide runtime data to combo box and when user submit I should get selected values from combo box and other fields. How should I design using MVP/MVC. Could any one provide some explanation or give me a link to any reference or example?
推荐答案
MVC 模式需要这 3 个类.
The MVC pattern requires these 3 classes.
- theModel:处理数据
- theView:处理 GUI
- theController:处理监听器.
您可能需要先了解观察者模式.
You may need some understanding of the Observer pattern first.
设计可能如下:
public class Test {
public Test(){
MyModel model = new MyModel();
MyController controller = new MyController(model);
}
}
控制器类
public class MyController implements ActionListener {
private MyView view;
private MyModel model;
MyController(MyModel theModel){
model = theModel;
view = new MyView(this);
model.register(view);
}
// implement actionPerformed here
}
查看类
public class View implements Observer {
private MyController controller;
public View(){
// Swing Components here
JButton button = new JButton();
button.addActionListener(controller);
add(button);
}
// notifyObserver method implementation
}
模型类
public class MyModel extends Observable {
// handle state/data changes here
}
这篇关于将 MVC/MVP 用于 Swing UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!