ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 3838|回复: 2

[原创]电脑培训:s1 JAVA MVC模式教学案例

[复制链接]

TA的精华主题

TA的得分主题

发表于 2007-6-12 23:14 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

电脑培训:s1 JAVA MVC模式教学案例
 
/*data.java
* Created on 2007-5-17
*
* To change the template for this generated file go to
* Window>references>Java>Code Generation>Code and Comments
*/
package mvcTest2;

/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>references>Java>Code Generation>Code and Comments
*/
public class Data
{
public int value; // 学生年龄值
public String name; // 学生名
}


/*model.java
* Created on 2007-5-17
*
* To change the template for this generated file go to
* Window>references>Java>Code Generation>Code and Comments
*/
package mvcTest2;

/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>references>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,name);
}
}
public Model(Data[] data)
{
for ( int i = 0; i< data.length; i++ )
{
addData(data);
}
}
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 2007-5-17
*
* To change the template for this generated file go to
* Window>references>Java>Code Generation>Code and Comments
*/
package mvcTest2;

/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>references>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.p***Int(jTextField1.getText());
data[1].name = jLabel3.getText();
data[1].value = Integer.p***Int(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 2007-5-17
*
* To change the template for this generated file go to
* Window>references>Java>Code Generation>Code and Comments
*/
package mvcTest2;

/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>references>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 2007-5-17
*
* To change the template for this generated file go to
* Window>references>Java>Code Generation>Code and Comments
*/
package mvcTest2;

/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>references>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();
}
}

 
转至:http://bbs.accp-teem.com.cn/showtopic-1622.html

TA的精华主题

TA的得分主题

发表于 2007-6-13 23:13 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

我是学JAVA的。初学者,虽然看不懂。。但是顶你一下。。呵呵。 。

TA的精华主题

TA的得分主题

发表于 2012-7-31 20:21 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-12-27 05:09 , Processed in 0.034164 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表