我有一个使用 MVC 模式开发 Java Swing 应用程序的想法。我在下面描述了我的想法,请告诉我,这是将 MVC 模式用于 java Swing 的正确方法吗?
- 这是视图
以下方法用于获取和设置上述视图的名称,
//at top of the view its model and controller is defined as
Model model = null;
Controller controller = null;
//constructor
public view(){
this.model = new Model();
this.controller = new Controller(this, model);//controller takes view and model as its parameters.
}
public void v_addShowNameButtonsActionListener(ActionListener al){
btnActionListener.addActionListener(al);
}
public String v_getName(){
return txtName.getText();// txtName is the name of the text field.
}
public void v_setName(String name){
txtName.setText(name);
}
- 这是控制器
/*at the top of the controller define the view and model*/ View view = null; Model model = null; /* constructor of the controller*/ public Constructor(View view, Model model){ this.view = view; this.model = model; } class CreateShowNameButtonsActionListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { Connection con = null; try{ con = ******************** /*get the data base connection*/ view.setName(model.m_getName(con)); }catch(Exception ex){ ex.printStackTrace(); }finally{ con.close(); } } }
- 这是模型
Public class Model{ public String m_getName(Connection con){ String name; name = ******* //do database queries and set get the name form the database return name; } }
我已经简要描述了我将在 java Swing 中实现 MVC 模式的方式。