?? simulator.java
字號:
/*
* 創建日期 2005-2-26
*/
package simulator;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Simulator extends JFrame implements ActionListener {
protected static final int NUM_AGENTS = 10; // 總服務代理的個數
protected static final int NUM_INITIAL_AGENTS = 6; // 初始服務代理的個數
protected static final int MAX_CUSTOMER_DELAY = 9000;
// 所允許的最大的顧客延時
protected static final int MAX_TELLER_BREAK = 1000;
//收銀員所需要的最大處理時間
protected static final int MAX_NO_CUSTOMERS = 2000; //最大的顧客號
//處理按鈕設置
private JButton open = new JButton("Open Doors");
private JButton close = new JButton("Close Doors");
private JButton add = new JButton("Add Handler");
private JButton del = new JButton("Del Handler");
/**
*
* @uml.property name="bank"
* @uml.associationEnd multiplicity="(1 1)"
*/
private Bank bank = new Bank(); //建立銀行和超市對象
/**
*
* @uml.property name="supermarket"
* @uml.associationEnd multiplicity="(1 1)"
*/
private Supermarket supermarket = new Supermarket();
//窗口關閉處理程序
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent we) {
bank.stop(); //關閉超市和銀行的模擬線程
supermarket.stop();
System.exit(0); //退出到系統
}
}
public Simulator() { //構造方法
super("Simluation Bank vs Supermarket"); //顯示標題
JPanel buttons = new JPanel(); //創建一個新的面板
buttons.setLayout(new FlowLayout()); //版面設置
buttons.add(open);
open.addActionListener(this);
buttons.add(close);
close.addActionListener(this);
buttons.add(add);
add.addActionListener(this);
buttons.add(del);
del.addActionListener(this);
addWindowListener(new WindowCloser());
getContentPane().add(bank, BorderLayout.WEST);
getContentPane().add(supermarket, BorderLayout.EAST);
getContentPane().add(buttons, BorderLayout.SOUTH);
validate();
pack();
show();
bank.start(); //啟動銀行模擬線程
supermarket.start(); //啟動超市模擬線程
}
//根據單擊的是哪一個按鈕而執行相應的操作
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == open) {
bank.openDoor(); //銀行開門
supermarket.openDoor(); //超市開門
} else if (ae.getSource() == close) {
bank.closeDoor(); //銀行關門
supermarket.closeDoor(); //超市關門
} else if (ae.getSource() == add) {
bank.addAgent(); //銀行增加服務代理;
supermarket.addAgent(); //超市增加服務代理;
} else if (ae.getSource() == del) {
bank.retireAgent(); //銀行刪除服務代理;
supermarket.retireAgent(); //超市刪除服務代理;
}
}
public static void main(String args[]) {
Simulator smlt = new Simulator();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -