simulator.java

来自「java编程开发技巧与实例的编译测试通过的所有例程」· Java 代码 · 共 77 行

JAVA
77
字号
import java.awt.*;
import java.awt.event.*;

public class Simulator extends Frame 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	Button	open	=	new Button("Open Doors");
	private	Button	close	=	new Button("Close Doors");
	private	Button	add		=	new Button("Add Handler");
	private	Button	del		=	new Button("Del Handler");
	private	Bank	bank	=	new Bank();
	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("Simulation bank vs supermarket");
		Panel buttons	=	new Panel();
		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());
		setLayout(new BorderLayout());
		add("West", bank);
		add("East", supermarket);
		add("South", buttons);
		validate();
		pack();
		show();
		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 + =
减小字号Ctrl + -
显示快捷键?