Mediator在Java Swing中的作用
2009-06-17 00:23
393 查看
Mediator在Swing中可以充当MVC中的Controller的角色,例如:
public class FrmUserManager extends JFrame {
private UserManagerMediator mediator;
private JButton addBtn = new JButton();
private JButton delBtn = new JButton();
private JComboBox userComboBox = new JComboBox();
// 下面是其他一些控件
public FrmUserManager() {
mediator = new UserManagerMediator(this);
initComponent();
}
private initComponent() {
addBtn.addActionListener(mediator.addAction());
delBtn.addActionListener(mediator.deleteAction());
userComboBox.setModel(mediator.userComboBoxModel());
// other operation
}
public JComboBox getUserComboBox() {
return userComboBox;
}
}
public class UserManagerMediator {
private FrmUserManager frmUserManager;
public UserManagerMediator(FrmUserManager frmUserManager) {
this.frmUserManager = frmUserManager;
}
public ActionListener addAction() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox userComboBox = frmUserManager.getUserComboBox();
DefaultComboBoxModel model = (DefaultComboBoxModel)userComboBox.getModel();
model.addElement(......);
}
};
}
public ActionListener deleteAction() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox userComboBox = frmUserManager.getUserComboBox();
DefaultComboBoxModel model = (DefaultComboBoxModel)userComboBox.getModel();
model.removeElement(......);
}
};
}
public DefaultComboBoxModel userComboBoxModel() {
// 比如:查询数据库得到数据
DefaultComboBoxModel model = new DefaultComboBoxModel();
// 添加数据
model.addListDataListener(new ListDataListener() {
// 实现一些方法
});
return model;
}
}
View--FrmUserManager,Controller--UserManagerMediator,Model--在控制器中调用
当View的状态改变(如:点击按钮,选择下拉框),调用Mediator中相应方法来更新Model;当Model改变(如:下拉框增加一项),会通过Mediator,更新视图。
public class FrmUserManager extends JFrame {
private UserManagerMediator mediator;
private JButton addBtn = new JButton();
private JButton delBtn = new JButton();
private JComboBox userComboBox = new JComboBox();
// 下面是其他一些控件
public FrmUserManager() {
mediator = new UserManagerMediator(this);
initComponent();
}
private initComponent() {
addBtn.addActionListener(mediator.addAction());
delBtn.addActionListener(mediator.deleteAction());
userComboBox.setModel(mediator.userComboBoxModel());
// other operation
}
public JComboBox getUserComboBox() {
return userComboBox;
}
}
public class UserManagerMediator {
private FrmUserManager frmUserManager;
public UserManagerMediator(FrmUserManager frmUserManager) {
this.frmUserManager = frmUserManager;
}
public ActionListener addAction() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox userComboBox = frmUserManager.getUserComboBox();
DefaultComboBoxModel model = (DefaultComboBoxModel)userComboBox.getModel();
model.addElement(......);
}
};
}
public ActionListener deleteAction() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox userComboBox = frmUserManager.getUserComboBox();
DefaultComboBoxModel model = (DefaultComboBoxModel)userComboBox.getModel();
model.removeElement(......);
}
};
}
public DefaultComboBoxModel userComboBoxModel() {
// 比如:查询数据库得到数据
DefaultComboBoxModel model = new DefaultComboBoxModel();
// 添加数据
model.addListDataListener(new ListDataListener() {
// 实现一些方法
});
return model;
}
}
View--FrmUserManager,Controller--UserManagerMediator,Model--在控制器中调用
当View的状态改变(如:点击按钮,选择下拉框),调用Mediator中相应方法来更新Model;当Model改变(如:下拉框增加一项),会通过Mediator,更新视图。
相关文章推荐
- Java GUI编程SwingUtilities.invokeLater作用
- Java GUI编程SwingUtilities.invokeLater作用
- JAVA中SWing的作用?
- Java equals()和hashCode()的作用
- java中awt和swing是的关系以及区别
- java swing 树型结构
- Java Swing中JTable的使用
- JAVA基础:eclipse.ini中各个参数的作用计算机二级考试
- Java Swing 中三种事件处理方法之比较
- java中FileInputStream的available() 方法的作用
- Java中AWT、Swing与SWT三大GUI技术的原理与效率差异
- java开发过程中遇到的一些jar包的作用
- 【003】【深入解析Java中volatile关键字的作用】
- Java中JDK和JRE的区别是什么?它们的作用分别是什么?
- JavaSwing图形界面编程之JTable
- JavaSwing图形界面编程之KeyListener
- Java - 监听器有哪些作用和用法?
- java swing隐藏按钮的方法
- Java中AWT和SWing的区别与联系
- Java开发环境配置的作用