两种不同的实现mvc模式(1)
view接口,供所有view 实现
public interface View { public void refreshContactView(String firstName, String lastName); }model java 代码
import java.util.ArrayList; import java.util.Iterator; public class TextModel { private String firstName; private String lastName; private ArrayList contactViews = new ArrayList();//用来存不同的视图的容器 public TextModel(){ this(null); } public TextModel(View view){ firstName = ""; lastName = ""; if (view != null){ contactViews.add(view); } } public void addContactView(View view){ if (!contactViews.contains(view)){ contactViews.add(view); } } public void removeContactView(View view){ contactViews.remove(view); } public void updateModel(String newFirstName, String newLastName){ if (!isEmptyString(newFirstName)){ setFirstName(newFirstName); } if (!isEmptyString(newLastName)){ setLastName(newLastName); } updateView(); } private void updateView(){ Iterator notifyViews = contactViews.iterator(); while (notifyViews.hasNext()){ ((View)notifyViews.next()).refreshContactView(firstName, lastName); } } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public void setFirstName(String newFirstName){ firstName = newFirstName; } public void setLastName(String newLastName){ lastName = newLastName; } private boolean isEmptyString(String input){ return ((input == null) || input.equals("")); } }用来修改的视图 构造里传入model ,同时启动控制器类
public class EditView extends JPanel implements View{ private static final String UPDATE_BUTTON = "Update"; private static final String EXIT_BUTTON = "Exit"; private static final String CONTACT_FIRST_NAME = "First Name "; private static final String CONTACT_LAST_NAME = "Last Name "; private static final int FNAME_COL_WIDTH = 25; private static final int LNAME_COL_WIDTH = 40; private EditController controller; private JLabel firstNameLabel, lastNameLabel; private JTextField firstName, lastName; private JButton update, exit; public EditView(TextModel model) { controller = new EditController(model,this); createGui(); } public void createGui() { update = new JButton(UPDATE_BUTTON); exit = new JButton(EXIT_BUTTON); firstNameLabel = new JLabel(CONTACT_FIRST_NAME); lastNameLabel = new JLabel(CONTACT_LAST_NAME); firstName = new JTextField(FNAME_COL_WIDTH); lastName = new JTextField(LNAME_COL_WIDTH); JPanel editPanel = new JPanel(); editPanel.setLayout(new BoxLayout(editPanel, BoxLayout.X_AXIS)); JPanel labelPanel = new JPanel(); labelPanel.setLayout(new GridLayout(0, 1)); labelPanel.add(firstNameLabel); labelPanel.add(lastNameLabel); editPanel.add(labelPanel); JPanel fieldPanel = new JPanel(); fieldPanel.setLayout(new GridLayout(0, 1)); fieldPanel.add(firstName); fieldPanel.add(lastName); editPanel.add(fieldPanel); JPanel controlPanel = new JPanel(); controlPanel.add(update); controlPanel.add(exit); update.addActionListener(controller); exit.addActionListener(new Handler()); setLayout(new BorderLayout()); add(editPanel, BorderLayout.CENTER); add(controlPanel, BorderLayout.SOUTH); } public Object getUpdateRef() { return update; } public String getFirstName() { return firstName.getText(); } public String getLastName() { return lastName.getText(); } public void refreshContactView(String newFirstName, String newLastName) { // TODO 自动生成方法存根 firstName.setText(newFirstName); lastName.setText(newLastName); } private class Handler implements ActionListener { public void actionPerformed(ActionEvent event) { System.exit(0); } } }控制类,把model类和View类注册进来,通过这个类去改变model,model改变时 * 调用自身的update方法,更新view
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class EditController implements ActionListener { private TextModel model; private EditView view; public EditController(TextModel model2, EditView view2) { // TODO 自动生成构造函数存根 view = view2; model = model2; } public void actionPerformed(ActionEvent evt){ Object source = evt.getSource(); if (source == view.getUpdateRef()){ updateModel(); } } private void updateModel(){ String firstName = null; String lastName = null; if (view.getFirstName()!=null){ firstName = view.getFirstName(); } if (view.getLastName()!=null){ lastName = view.getLastName(); } model.updateModel( firstName, lastName); } }用来显示model数据视图
import java.awt.BorderLayout; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; class DisplayView extends JPanel implements View { private JTextArea display; public DisplayView() { createGui(); } public void createGui() { setLayout(new BorderLayout()); display = new JTextArea(10, 40); display.setEditable(false); JScrollPane scrollDisplay = new JScrollPane(display); this.add(scrollDisplay, BorderLayout.CENTER); } public void refreshContactView(String newFirstName, String newLastName) { display.setText("UPDATED CONTACT:\nNEW VALUES:\n" + "\tName: " + newFirstName + " " + newLastName + "\n"); } }整个mvc用到model controller view 都是联系起来的
*model-->view
*view--->model
*controller把它们联系起来
import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.Serializable; import javax.swing.JFrame; import javax.swing.JPanel; public class RunMVCPattern { public static void main(String [] arguments){ TextModel model = new TextModel(); EditView editorView = new EditView(model);///增加视图 model.addContactView(editorView); createGui(editorView, "Contact Edit Window"); System.out.println("Creating DisplayView"); DisplayView displayView = new DisplayView(); model.addContactView(displayView); createGui(displayView, " Display Window"); } private static void createGui(JPanel contents, String title){ JFrame applicationFrame = new JFrame(title); applicationFrame.getContentPane().add(contents); applicationFrame.pack();//自动适应大小 applicationFrame.setVisible(true); } }
相关推荐
URML 2020-07-05
洗尽铅华 2020-06-07
Enbiting 2020-06-03
howema 2020-05-09
rola0 2020-04-29
闲来也无事 2020-04-20
zhaojp0 2020-04-11
yonger 2020-03-15
yusongwhu 2020-02-23
codercheng 2020-01-18
laofangzi 2020-01-14
spinachcqb 2019-12-31
洗尽铅华 2019-12-23
爱好HtmlCssJs 2019-12-04
mvc0 2019-11-30
闲来也无事 2019-12-02
探秘 2019-11-20
liuweiyan 2019-11-10