CSDN博客

img csdnb3a

Java 实现MVC模式的例子

发表于2004/6/24 17:09:00  3104人阅读

分类: Java


/*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();
}
}


0 0

相关博文

我的热门文章

img
取 消
img