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

📄 objbalances.java

📁 介绍有关java的资料 课件 相当一本书籍 里面都是很基础的知识
💻 JAVA
字号:
package objbalances;// 多个出纳员对多个账户同时操作而让账户保持平衡。import java.awt.*;import java.util.*;class ObjBalances extends Frame {  Label names[];  TextField sbalances[];  Button start,stop,exit;  int sum;  Customer customers[];  Teller teller1, teller2, teller3;  //-----------------------------------  public ObjBalances() {    super("银行平衡表");    setLayout(new BorderLayout());    setBackground(Color.lightGray);    Panel p1 = new Panel();    Panel p2 = new Panel();    add("South", p2);    add("Center", p1);    p1.setLayout(new GridLayout(6, 2));    sbalances = new TextField[6];    sum = 0;    names = new Label[6];    names[0] = new Label("张三");    names[1]= new Label("李四");    names[2] = new Label("王五");    names[3] = new Label("赵六");    names[4] = new Label("侯七");    names[5] = new Label("总计");    customers = new Customer[5];    for (int i =0; i< 5; i++) {      customers[i]= new Customer(1000);      int b = customers[i].getBalance();      sum += b;      sbalances[i] = new TextField(new Integer(b).toString());      Panel p = new Panel();      p1.add(p);      p1.add(names[i]);      p.add(sbalances[i]);      p1.add(p);    }    sbalances[5] = new TextField(new Float(sum).toString());    Panel p = new Panel();    p1.add(names[5]);    p1.add(p);    p.add(sbalances[5]);    start = new Button("开始");    stop = new Button("停止");    exit = new Button("退出");    p2.add(start);    p2.add(stop);    p2.add(exit);    reshape(100,100,300,250);    show();    }    //-----------------------------------    public void paint(Graphics g) {    float sum =0;    for (int i=0; i< 5; i++) {      int b = customers[i].getBalance();      sbalances[i].setText(new Integer(b).toString());      sum += b;    }    sbalances[5].setText( new Float(sum).toString());  }  //-----------------------------------  public void clickedStart() {    teller1 = new Teller(customers, 5, 1);    teller2 = new Teller(customers, 5, 5);    teller3 = new Teller(customers, 5, 7);    teller1.start();    teller2.start();    teller3.start();    start.disable();  }  //-----------------------------------  public void clickedStop() {    teller1.stoppit();    teller2.stoppit();    teller3.stoppit();    start.enable();    repaint();  }  //-----------------------------------  public boolean action(Event evt, Object arg) {    if (evt.target == start){      clickedStart();      return true;    }    else      if (evt.target == stop) {        clickedStop();        return true;      }      else        if(evt.target == exit){          System.exit(0);          return true;        }        else          return super.action(evt, arg);  }  //-----------------------------------  public static void main(String argv[]) {    new ObjBalances();  }}//==========================================class Customer {  int balance;  //-----------------------------------  public Customer(int money) {    balance = money;  }  //-----------------------------------  // synchronized public void changeLedger(int value)  public void changeLedger(int value) {    balance += value;  }  //-----------------------------------  public int getBalance() {    return balance;  }}//==========================================class Teller extends Thread {  int count;  int amount;  Customer cust[];  boolean stop_flag;  //-----------------------------------  public Teller(Customer cst[], int cnt, int amt) {    count = cnt;    amount = amt;    cust = cst;    stop_flag = false;  }  //-----------------------------------  public void stoppit() {    stop_flag = true;  }  //-----------------------------------  public void run() {    Customer custf, custto;    while (true) {      int from = (int)Math.round(Math.random() *(count-1));      int to = (int)Math.round(Math.random() *(count-1));      if (from != to) {        custf = (Customer)cust[from];        custto = (Customer)cust[to];        synchronized(custf) {          custf.changeLedger(-amount);        }        synchronized(custto) {          custto.changeLedger(amount);        }      } //end if      if (stop_flag) stop();    } //end while  }}

⌨️ 快捷键说明

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