⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yinhangzhanghu.txt

📁 JavaBean的自定义事件:编写一个表示银行帐户的JavaBean
💻 TXT
字号:
1)编写事件类
与事件发生有关的状态信息都封装在这个事件对象中,这个对象应该是java.util.EventObjec的子类。
参考代码:
import java.util.EventObject;
public class AccountChangeEvent    extends EventObject {
  public static final int DEPOSIT = 1;
  public static final int WITHDRAW = -1;
  double amountChange = 0;
  int method = 0;
  public AccountChangeEvent(Object source, int method, double change) {
    super(source);
    this.method = method;
    amountChange = change;
  }
  public int getMethod() {
    return method;
  }
  public double getAmountChange() {
    return amountChange;
  }
  public String toString() {
    String s = "";
    if (method == DEPOSIT) {
      s = "存款:" + amountChange;
    }
    else if (method == WITHDRAW) {
      s = "取款:" + amountChange;
    }
    else {
      s = "非法操作!";
    }
    return s;
  }

}
2)编写监听器接口和实现类
在JavaBean中,事件操纵方法都被定义在继承了java.uitl.EventListener接口的监听器接口中,并且以Listener结尾。
参考代码:
import java.util.EventListener;
public interface AccountChangeListener
    extends EventListener {
  void accountChange(AccountChangeEvent e);
}

public class MyAccountListener
    implements AccountChangeListener {
  public void accountChange(AccountChangeEvent e) {
    System.out.print("事件对象中的信息:事件源"+e.getSource().getClass().getName());
    System.out.print("发生了账户变化事件,");
    System.out.println(e.toString());

  }
}

3)编写Bean
Bean作为事件源要提供注册和取消事件监听器的方法。

参考代码:
import java.util.*;
public class MyAccountBean {
  private double balance = 0.0;
  private int lastMethod = 0;
  private double change = 0.0;
  private Vector listeners = new Vector();
  public double getBalance() {
    return balance;
  }
 public void setBalance(double balance) {
    this.balance = balance;
  }
  public double withdraw(double amount) {
    setBalance(balance - amount);
    setChange(amount);
    lastMethod = AccountChangeEvent.WITHDRAW;
    notifyAccountChange();
    return balance;
  }
  public double deposit(double amount) {
    setBalance(balance + amount);
    setChange(amount);
    lastMethod = AccountChangeEvent.DEPOSIT;
    notifyAccountChange();
    return balance;
  }
  public synchronized void addAccountChangeListener(AccountChangeListener l) {
    listeners.add(l);
  }
  public synchronized void removeAccountChangeListener(AccountChangeListener l) {
    listeners.removeElement(l);
  }
  protected void notifyAccountChange() {
    Vector l;
    AccountChangeEvent e = new AccountChangeEvent(this, lastMethod, change);
    synchronized (this) {
      l = (Vector) listeners.clone();
    }
    for (int i = 0; i < l.size(); i++) {
      ( (AccountChangeListener) l.elementAt(i)).accountChange(e);
    }
  }
  public void setChange(double change) {
    this.change = change;
  }
  public String toString() {
    String s = "余额:" + balance + ";";
    if (lastMethod == AccountChangeEvent.DEPOSIT) {
      s = "存款:" + change + ";" + s;
    }
    else if (lastMethod == AccountChangeEvent.WITHDRAW) {
      s = "取款:" + change + ";" + s;
    }
    else {
      s = "非法操作!" + s;
    }
    return s;
  }
}
4)编写测试程序
测试程序创建一个Bean的对象,通过输出结果,观察事件变化。

参考代码:
public class MyAccountTest {
  public static void main(String[] args) {
    MyAccountListener l = new MyAccountListener();
    MyAccountBean bean1 = new MyAccountBean();
    bean1.addAccountChangeListener(l);
    bean1.deposit(300);
    System.out.println("事件源的状态: "+bean1.toString());
    bean1.withdraw(200);
    System.out.println("事件源的状态: "+bean1.toString());
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -