·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » MVC-OBSERVER模式

MVC-OBSERVER模式

类别: JSP教程  评论数:0 总得分:0
package mvc_observer;

/**
* 此处插入类型说明。
* 创建日期:(2002-9-4 13:18:39)
* @author:Administrator
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;

import javax.swing.event.*;import java.util.*;public class Controller extends JFrame {
Model model = null;


JScrollPane jScrollPane1 = new JScrollPane();

JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
public Controller() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {

ArrayList data = new ArrayList();
Message mess1 = new Message();
mess1.name = "John";
mess1.value = 20;
data.add(mess1);
Message mess2 = new Message();
mess2.name = "Smith";
mess2.value = 14;
data.add(mess2);

model = new Model(data);
viewText = new ViewText(model);
viewGraph = new ViewGraph(model);

// 注意下面两行:向模型中登记它的观察者View1和View2.
model.addObserver(viewText);
model.addObserver(viewGraph);

//model.registerObserver(viewText);
//model.registerObserver(viewGraph);
this.getContentPane().setLayout(null);
jScrollPane1.setBounds(new Rectangle(0, 0, 3, 3));

jTextField1.setText(String.valueOf(((Message)data.get(0)).value));
jTextField1.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
// 如果改变了内容,就可以即时更新 label 显示的内容
textChangeEvent(e);
}
});

jTextField1.setBounds(new Rectangle(80, 254, 52, 30));
jTextField2.setText(String.valueOf(((Message)data.get(1)).value));
jTextField2.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
// 如果改变了内容,就可以即时更新 label 显示的内容
textChangeEvent(e);
}
});
jTextField2.setBounds(new Rectangle(178, 255, 50, 31));
jLabel1.setText("提示信息:");
jLabel1.setBounds(new Rectangle(41, 226, 200, 23));
jLabel2.setText(((Message)data.get(0)).name);
jLabel2.setBounds(new Rectangle(42, 252, 30, 33));
jLabel3.setText(((Message)data.get(1)).name);
jLabel3.setBounds(new Rectangle(140, 252, 90, 33));

viewText.setBounds(new Rectangle(7, 5, 225, 208));
viewGraph.setBounds(new Rectangle(234, 5, 219, 208));

this.getContentPane().add(jScrollPane1, null);
this.getContentPane().add(jTextField2, null);
this.getContentPane().add(jTextField1, null);
this.getContentPane().add(jLabel2, null);
this.getContentPane().add(jLabel3, null);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(viewText, null);
this.getContentPane().add(viewGraph, null);

}


public static void main(String[] args) {

Controller c = new Controller();
c.setSize(475, 330);
c.setVisible(true);
}

ViewGraph viewGraph = null; ViewText viewText = null; void textChangeEvent(CaretEvent e) {
try{


ArrayList data = new ArrayList();
Message mess1 = new Message();
mess1.name = jLabel2.getText();
mess1.value = Integer.parseInt(jTextField1.getText());
data.add(mess1);
Message mess2 = new Message();
mess2.name = jLabel3.getText();
mess2.value = Integer.parseInt(jTextField2.getText());
data.add(mess2);

int value1 = Integer.parseInt(jTextField1.getText());
int value2 = Integer.parseInt(jTextField2.getText());

if((value1>0&&value1<100)&&(value2>0&&value2<100)){
model.setData(data);

}
else {
jLabel1.setText("提示信息:年龄在1到100之间!");
}


}catch(Exception ex){
//ex.printStackTrace();
jLabel1.setText("提示信息:年龄在1到100之间!");
}

}}
---------------------------------------------------------------------------------------------------------------------
package mvc_observer;

/**
* 此处插入类型说明。
* 创建日期:(2002-9-4 13:18:39)
* @author:Administrator
*/
public class Message {
public int value;
public String name;


}
------------------------------------------------------------------------------------------------------------------
package mvc_observer;

import java.util.*;
/**
* 此处插入类型说明。
* 创建日期:(2002-9-4 13:18:39)
* @author:Administrator
*/
public class Model extends Observable{
private ArrayList data = new ArrayList();

public Model(ArrayList data) {
for (int i = 0; i < data.size(); i++) {
this.data.add(data.get(i));
}
} public ArrayList getData() {
return data;
} public void setData(ArrayList data) {
this.data.clear();
for (int i = 0; i < data.size(); i++) {
this.data.add(data.get(i));
}
setChanged();
notifyObservers("name");
}
}
-------------------------------------------------------------------------------------------------------------
package mvc_observer;

/**
* 此处插入类型说明。
* 创建日期:(2002-9-4 13:18:39)
* @author:Administrator
*/
import javax.swing.*;

import java.awt.*;

import javax.swing.border.*;


import java.util.*;public class ViewGraph extends JPanel implements Observer {
private Model model;


public ViewGraph(Model model) {
try {
this.model = model;
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
this.setBackground(Color.white);
this.setBorder(new TitledBorder(BorderFactory.createLineBorder(Color.black, 1),"View1"));

}

public void paintComponent(Graphics g) {

super.paintComponent(g);
if (model == null){
return;
}

int x = 20, y = 50;
int h = g.getFontMetrics().getHeight();
int width = this.getWidth();
int height = this.getHeight();
int sy = height / model.getData().size();
int sx = width / 2;
for (int i = 0; i < model.getData().size(); i++) {
Message data = (Message)model.getData().get(i);
int value = data.value;
int dx = 3;
int r = 3;
Color c = new Color((int) (255 * Math.random()), (int)(255 * Math.random()),(int)(255 * Math.random()));
int cx = sx;
int cy = y + i * sy;
for (int j = 0; j < value; j++) {
g.setColor(c);
g.drawOval(cx, cy, r, r);
r += dx;
}
g.drawString(data.name, 25, cy);
}
}


// 当模型数据发生改变时,会自动调用此方法来刷新图形

public void update(Observable obj,Object args) {
/**@todo: Implement this Observer method*/
this.model = model;
repaint();
}}
-------------------------------------------------------------------------------------------------------------
package mvc_observer;

/**
* 此处插入类型说明。
* 创建日期:(2002-9-4 13:18:39)
* @author:Administrator
*/
import javax.swing.*;

import java.awt.*;

import javax.swing.border.*;

import java.util.*;public class ViewText extends JPanel implements Observer {
private Model model;


public ViewText(Model model) {
try {
this.model = model;
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
this.setBackground(Color.white);
this.setBorder(new TitledBorder(BorderFactory.createLineBorder(Color.black, 1), "View1"));
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
if (model == null){
return;
}
int x = 20, y = 50;
int h = g.getFontMetrics().getHeight();
for (int i = 0; i < model.getData().size(); i++) {
Message data = (Message)model.getData().get(i);
g.drawString(data.name, x, y);
y += h;
g.drawString(String.valueOf(data.value), x, y);
y += h;
}
}


public void update(Observable obj,Object args) {
/**@todo: Implement this Observer method*/
this.model = model;
repaint();
}
}
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1