- lindongxiao
- 等级: 初级会员
- 性别:
- 文章: 29
- 积分: 92
- 来自: 泉州
|
http://lindongxiao.iteye.com/admin/show/98810这里有另外一种实现,可以对比
view接口
java 代码
- public interface View {
- public void refreshContactView(String firstName, String lastName);
- }
java 代码
- 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);
- }
- }
-
- }
显示view java 代码
- 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(""));
- }
-
-
- }
控制类
java 代码
- 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
java 代码
- 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.pack();
-
-
- applicationFrame.setVisible(true);
- }
-
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|