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

MVC模式

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

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;

import javax.swing.event.*;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 {

Data[] data = new Data[2];
data[0] = new Data();
data[1] = new Data();
data[0].name = "John";
data[0].value = 20;
data[1].name = "Smith";
data[1].value = 14;
model = new Model(data);
viewText = new ViewText(model);
viewGraph = new ViewGraph(model);

// 注意下面两行:向模型中登记它的观察者View1和View2.

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

jTextField1.setText(String.valueOf(data[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(data[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(data[0].name);
jLabel2.setBounds(new Rectangle(42, 252, 30, 33));
jLabel3.setText(data[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{

Data[] data = new Data[2];
data[0] = new Data();
data[1] = new Data();
data[0].name = jLabel2.getText();
int value1 = Integer.parseInt(jTextField1.getText());
data[0].value = Integer.parseInt(jTextField1.getText());
data[1].name = jLabel3.getText();
data[1].value = Integer.parseInt(jTextField2.getText());
int value2 = Integer.parseInt(jTextField2.getText());
if((value1>0&&value1<100)&&(value2>0&&value2<100)){
Model m = new Model(data);
this.model.changeModel(m);
}
else {
jLabel1.setText("提示信息:年龄在1到100之间!");
}


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

}}
----------------------------------------------------------------------------
package mvc;

public class Data {
public int value;
public String name;


}
-----------------------------------------------------------------------------
package mvc;

public class Model {
ArrayList data = new ArrayList();
ArrayList observer = new ArrayList();


public Model(Data[] data) {
for (int i = 0; i < data.length; i++) {
this.data.add(data);
}
}



public Data getData(int idx) {
return (Data) (data.get(idx));
}

public int size() {
return data.size();
}

// 用来向模型中登记观察者.

public void registerObserver(Observer o) {
observer.add(o);
}

public void removeObserver(Observer o) {
observer.remove(o);
}

// 当数据改变时,由Controller调用此方法,通知各个Observer,刷新视图.

public void changeModel(Model model) {
data.clear();
for (int i = 0; i < model.size(); i++) {
this.data.add(model.data.get(i));
}
dataUpdate();
}

private void dataUpdate() {
for (Iterator i = observer.iterator(); i.hasNext();) {
Observer o = (Observer) (i.next());
o.dataUpdate(this);
}
}

}
-------------------------------------------------------------------------
package mvc;

public interface Observer {
public void dataUpdate(Model model);
}
------------------------------------------------------------------------------

import javax.swing.*;

import java.awt.*;

import javax.swing.border.*;


public class ViewGraph extends JPanel implements Observer {
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.size();
int sx = width / 2;
for (int i = 0; i < model.size(); i++) {
Data data = model.getData(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 dataUpdate(Model model) {
/**@todo: Implement this Observer method*/
this.model = model;
repaint();
}

}
---------------------------------------------------------------------------------
package mvc;

import javax.swing.*;

import java.awt.*;

import javax.swing.border.*;

public class ViewText extends JPanel implements Observer {
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.size(); i++) {
Data data = model.getData(i);
g.drawString(data.name, x, y);
y += h;
g.drawString(String.valueOf(data.value), x, y);
y += h;
}
}

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

public void dataUpdate(Model model) {
/**@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