为了更好理解MVC一例

<script type="text/javascript"> google_ad_client = "pub-8800625213955058"; /* 336x280, 创建于 07-11-21 */ google_ad_slot = "0989131976"; google_ad_width = 336; google_ad_height = 280; // </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> 为了更好理解MVC,在这里我给出一个简单例子. 共有4个java类,你可以Run AccountFrame.java.




//AccountModel.java

package com.newmodern.mvc;



import java.util.*;



public class AccountModel extends Observable{



	private double dBalance;

    private String AccountID;



	public AccountModel(String AccountID, double dBalance){

		this.AccountID = AccountID;

        this.dBalance = dBalance;

	}



	public void deposit(double dBalance){

		this.dBalance =dBalance;

		setChanged();

		notifyObservers(this);

	}



	public void substract(double dBalance){

		this.dBalance-=dBalance;

		setChanged();

		notifyObservers(this);

	}



    public double getBalance(){

		return dBalance;

  	}



    public void mynotifyObservers(){

        setChanged();

        notifyObservers(this);

    }



}






//AccountView.java

package com.newmodern.mvc;



import java.util.*;

import java.awt.*;



public class AccountView implements Observer{



	TextField txtBalance;

	AccountModel am;



	public  AccountView (AccountModel am){

        this.am = am;

		am.addObserver(this);

		txtBalance = new TextField(10);

	}



	public void update(Observable o,Object arg){

		txtBalance.setText(String.valueOf(am.getBalance()));

	}



    public TextField getTextField(){

		return txtBalance;

	}



}




package com.newmodern.mvc;



import java.awt.event.*;



public class AccountController implements ActionListener{

	AccountFrame accountFrame;

	public AccountController(AccountFrame af){

		this.accountFrame = af;

	}

    public void actionPerformed(ActionEvent ae)   {

        String sCommand = ae.getActionCommand();

        if (sCommand.equals("deposit")){

            System.out.println("Action: deposit");

            accountFrame.am.deposit(accountFrame.getValue());

        } else if(sCommand.equals("sub")){

            System.out.println("Action: substract");

            accountFrame.am.substract(accountFrame.getValue());

        }

	}

}






package com.newmodern.mvc;

import java.awt.*;

import java.awt.event.*;



public class AccountFrame extends Frame {



	AccountController ac;

	AccountView       av;

	AccountView       av1;

	AccountModel	  am;



	TextField txtValue;

	Button    btnDeposit;

	Button    btnSub;

	public AccountFrame (String sTitle)

        {

		super(sTitle);



		addWindowListener(new WindowAdapter(){

			public void windowClosing(WindowEvent we){

				dispose();

				System.exit(0);

			}

		});

		ac = new AccountController(this);

		am = new AccountModel(sTitle,0);

		av = new AccountView(am);



		av1 = new AccountView(am);



		setLayout(new GridLayout(4,2));

		txtValue = new TextField(10);

		btnDeposit = new Button("deposit");

		btnSub     = new Button("sub");

		add(new Label("value:"));

		add(txtValue);

		add(new Label("balance:"));

		add(av.getTextField());

		add(new Label("balance1:"));

		add(av1.getTextField());



		add(btnDeposit);

		add(btnSub);

		btnDeposit.addActionListener(ac);

		btnSub.addActionListener(ac);

		//setSize(200,200);

		pack();

		show();

                am.mynotifyObservers();

	}

        public double getValue() throws NumberFormatException {



		double value = Double.parseDouble(txtValue.getText());

		return value;

	}





	public static void main(String args[])

        {

		String id = "100";

                new AccountFrame(id);

	}



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值