两种不同的实现mvc模式(2) 使用Observer实现
public interface View { public void refreshContactView(String firstName, String lastName); }
import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import mvcpattern1.EditController; 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) { firstName.setText(newFirstName); lastName.setText(newLastName); } private class Handler implements ActionListener { public void actionPerformed(ActionEvent event) { System.exit(0); } } }
import java.awt.BorderLayout; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import java.util.*; class DisplayView extends JPanel implements View,Observer{ 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" ); } public void update(Observable o, Object arg) { if (arg instanceof TextModel){ String firstName=((TextModel)arg).getFirstName(); String lastName=((TextModel)arg).getLastName(); this.refreshContactView(firstName, lastName); } } }model java 代码
import java.util.Observable; public class TextModel extends Observable { private String firstName; private String lastName; public TextModel(){ } public void updateModel(String newFirstName, String newLastName) { if (!isEmptyString(newFirstName)) { setFirstName(newFirstName); } if (!isEmptyString(newLastName)) { setLastName(newLastName); } } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public void setFirstName(String newFirstName) { firstName = newFirstName;// 设置变化点 setChanged(); notifyObservers(this); } public void setLastName(String newLastName) { lastName = newLastName;// 设置变化点 setChanged(); notifyObservers(this); } private boolean isEmptyString(String input) { return ((input == null) || input.equals("")); } }控制类
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class EditController implements ActionListener { private TextModel model; private EditView view; public EditController(TextModel m, EditView v){ model = m; view = v; } 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); } }运行mvc
import javax.swing.JFrame; import javax.swing.JPanel; public class RunMVCPattern { public static void main(String [] arguments){ DisplayView displayView = new DisplayView(); TextModel model = new TextModel(); model.addObserver( displayView); EditView editorView = new EditView(model); createGui(editorView, "Contact Edit Window"); System.out.println("Creating DisplayView"); createGui(displayView, " Display Window"); } private static void createGui(JPanel contents, String title){ JFrame applicationFrame = new JFrame(title); applicationFrame.getContentPane().add(contents); // applicationFrame.setSize(400, 100); applicationFrame.pack();//自动适应大小 // applicationFrame.addWindowListener(new WindowClose()); // applicationFrame.setDefaultCloseOperation(applicationFrame.EXIT_ON_CLOSE); 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