分享
 
 
 

Java 实现MVC模式的例子

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

/*data.java

* Created on 2004-6-17

*

* To change the template for this generated file go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

package mvcTest2;

/**

* @author Administrator

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

public class Data

{

public int value; // 学生年龄值

public String name; // 学生名

}

/*model.java

* Created on 2004-6-17

*

* To change the template for this generated file go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

package mvcTest2;

/**

* @author Administrator

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

import java.util.*;

import java.util.Observable;

public class Model extends Observable

{

protected ArrayList data = new ArrayList();

public Model()

{

super();

}

public Model(int[] value, String[] name)

{

for ( int i = 0; i< value.length; i++ )

{

addData(value[i],name[i]);

}

}

public Model(Data[] data)

{

for ( int i = 0; i< data.length; i++ )

{

addData(data[i]);

}

}

public void addData(int value, String name)

{

Data data = new Data();

data.value = value;

data.name = name;

this.data.add(data);

setChanged(); // Indicates that the model has changed

notifyObservers(this);

}

public void addData(Data data)

{

this.data.add(data);

setChanged(); // Indicates that the model has changed

notifyObservers(this);

}

public Data getData(int idx)

{

return (Data)(data.get(idx));

}

public int size()

{

return data.size();

}

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

public void changeModel(Model model)

{

data.clear();

for (int i=0; i

{

this.addData(model.getData(i));

}

setChanged(); // Indicates that the model has changed

notifyObservers(this);

}

}

/* controller.java

* Created on 2004-6-17

*

* To change the template for this generated file go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

package mvcTest2;

/**

* @author Administrator

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

import java.awt.*;

import javax.swing.*;

//import javax.swing.border.*;

import java.awt.event.*;

public class Controller extends JFrame

{

Model model = new Model();

TextView txtView = new TextView(model);

GraphicsView graphView = new GraphicsView(model);

JScrollPane jScrollPane1 = new JScrollPane();

JButton jButton1 = new JButton();

JTextField jTextField1 = new JTextField();

JTextField jTextField2 = new JTextField();

JLabel jLabel1 = new JLabel();

JLabel jLabel2 = new JLabel();

JLabel jLabel3 = new JLabel();

public Controller()

{

try

{

super.setTitle("MVC parrten test");

jbInit();

}

catch (Exception e)

{

e.printStackTrace();

}

}

private void jbInit() throws Exception

{

Data[] data = new Data[4];

data[0] = new Data();

data[1] = new Data();

data[2] = new Data();

data[3] = new Data();

data[0].name = "Ted";

data[0].value = 20;

data[1].name = "Joy";

data[1].value = 14;

data[2].name = "Mars";

data[2].value = 23;

data[3].name = "Martian";

data[3].value = 25;

model.addData(data[0]);

model.addData(data[1]);

model.addData(data[2]);

model.addData(data[3]);

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

model.addObserver(txtView);

model.addObserver(graphView);

this.getContentPane().setLayout(null);

jScrollPane1.setBounds(new Rectangle(0, 0, 3, 3));

jButton1.setBounds(new Rectangle(309, 259, 101, 27));

jButton1.setText("Update");

jButton1.addActionListener(new java.awt.event.ActionListener()

{

public void actionPerformed(ActionEvent e)

{

jButton1_actionPerformed(e);

}

});

jTextField1.setText("20");

jTextField1.setBounds(new Rectangle(80, 254, 52, 30));

jTextField2.setText("14");

jTextField2.setBounds(new Rectangle(178, 255, 50, 31));

jLabel1.setText("Age:");

jLabel1.setBounds(new Rectangle(41, 226, 47, 23));

jLabel2.setText("Ted");

jLabel2.setBounds(new Rectangle(42, 252, 35, 33));

jLabel3.setText("Joy");

jLabel3.setBounds(new Rectangle(144, 255, 31, 31));

txtView.setBounds(new Rectangle(7, 5, 225, 208));

graphView.setBounds(new Rectangle(234, 4, 219, 209));

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(jButton1, null);

this.getContentPane().add(txtView, null);

this.getContentPane().add(graphView, null);

}

// 按下Update按钮,通知Model数据发生改变.

void jButton1_actionPerformed(ActionEvent e)

{

Data[] data = new Data[2];

data[0] = new Data();

data[1] = new Data();

data[0].name = jLabel2.getText();

data[0].value = Integer.parseInt(jTextField1.getText());

data[1].name = jLabel3.getText();

data[1].value = Integer.parseInt(jTextField2.getText());

Model m = new Model(data);

this.model.changeModel(m);

}

public static void main(String[] args)

{

Controller c = new Controller();

c.setSize(475, 350);

c.setVisible(true);

}

}

/* GraphView.java

* Created on 2004-6-17

*

* To change the template for this generated file go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

package mvcTest2;

/**

* @author Administrator

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

import javax.swing.*;

import java.awt.*;

import javax.swing.border.*;

import java.util.Observer;

import java.util.Observable;

public class GraphicsView extends JPanel implements Observer

{

Model model;

public GraphicsView()

{

}

public GraphicsView(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),"GraphicsView"));

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

if ( model == null ) return;

int x = 10,y = 30;

int h = g.getFontMetrics().getHeight();

int width = this.getWidth();

int height = this.getHeight();

int sy = height / (model.size() +1);

int sx = width/ 2 -40;

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

{

g.setColor(c);

g.drawOval(cx,cy,r,r);

r+=dx;

}

//*/

g.draw3DRect( cx,cy,value*2,15,true);

g.setColor(c);

g.fill3DRect(cx,cy,value*2,15,true );

g.drawString(data.name,25,cy+15);

}

}

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

public void update(Observable o, Object arg)

{

this.model =(Model) o;

repaint();

}

}

/*

* Created on 2004-6-17

*

* To change the template for this generated file go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

package mvcTest2;

/**

* @author Administrator

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

import javax.swing.*;

import java.awt.*;

import javax.swing.border.*;

import java.util.Observer;

import java.util.Observable;

public class TextView extends JPanel implements Observer

{

Model model;

public TextView()

{

}

public TextView(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),"TextView"));

}

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);

x+= g.getFontMetrics().stringWidth(data.name) + 20;

g.drawString(String.valueOf(data.value),x,y);

y+=h;

x = 20;

}

}

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

public void update(Observable o, Object arg)

{

this.model =(Model) o;

repaint();

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有