黑马程序员_银行管理系统

-------android培训java培训、期待与您交流!     ---------- 

自己在张老师讲的基础上加上了图形界面,在此与大家分享核心代码部分,欢迎大家指摘批评

1.程序运行界面截图

运行时界面

2.代码包截图


3.核心代码

public abstract class DisplayPanel extends JPanel
{	
	protected final JLabel titleLabel;
	protected final JTextArea displayTextArea = new JTextArea("textArea"); //display info
	public final JButton startButton = new JButton("start"); 
	public final JButton stopButton = new JButton("stop"); 
	protected final JLabel statusLabel = new JLabel("result:");// display the final result
	
	public DisplayPanel(String panelTitle)
	{
		this.titleLabel = new JLabel(panelTitle);
		this.setLayout(new BorderLayout());
		
		JPanel northPanel = new JPanel();
		northPanel.add(titleLabel);
		this.add(northPanel, BorderLayout.NORTH);
		
		displayTextArea.setEditable(false);
		this.add(new JScrollPane(displayTextArea, 
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, 
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS));
		
		JPanel southPanel = new JPanel(new GridLayout(1, 3, 10, 10));
		startButton.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{	
				displayTextArea.setText("");
				startButton.setEnabled(false);
				stopButton.setEnabled(true);
				startMethod();
			}

		});
		southPanel.add(startButton);
		
		stopButton.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{	
				startButton.setEnabled(true);
				stopButton.setEnabled(false);
				stopMethod();
				//System.out.println(Thread.currentThread().getName());//此处是事件调度线程
				
			}
		});
		southPanel.add(stopButton);
		
		southPanel.add(statusLabel);
		
		this.add(southPanel, BorderLayout.SOUTH);
		
	}

	protected abstract void stopMethod();

	protected abstract void startMethod();
	
	
}//end DisplayPanel

public class CommonQueuePanel extends DisplayPanel
{	
	//一个具体的Panel要与一个具体的worker对应
	private LineUpWorker lineWorker = 
		new LineUpWorker(CustomerType.Common, displayTextArea, 
				startButton, stopButton, statusLabel);
	
	public CommonQueuePanel(String panelTitle)
	{
		super(panelTitle);
	}

	@Override
	protected void stopMethod()
	{
		lineWorker.stop();
	}

	@Override
	protected void startMethod()
	{	
		lineWorker.start();
		lineWorker.execute();
	}

}

public class CommonWindowPanel extends DisplayPanel
{
	private ServiceWindowWorker serviceWindowWorker;
	
	public CommonWindowPanel(String panelTitle)
	{
		super(panelTitle);
		serviceWindowWorker = 
			new ServiceWindowWorker(new CommonServiceWindow(panelTitle), 
					displayTextArea, startButton, stopButton, statusLabel);
	}

	@Override
	protected void stopMethod()
	{
		serviceWindowWorker.stop();
	}

	@Override
	protected void startMethod()
	{	
		serviceWindowWorker.start();
		serviceWindowWorker.execute();
	}

}

/**
 * 产生三种类型的客户队列
 * @author whb
 *
 */
public class LineUpWorker extends SwingWorker<Void, String>
{
	private final LineUpMachine generator = LineUpMachine.getInstance();
	private final CustomerType customerType;
	private final JTextArea customerQueueTextArea ; //display the coming customer
	private final JButton startButton ; //start generator
	private final JButton stopButton ; //stop generator
	private final JLabel statusLabel;
	private boolean isRan = true;
	private boolean isFirstRan = true;
	private Thread backThread = null;
	
	public LineUpWorker(CustomerType customerType, JTextArea customerQueueTextArea, 
			JButton startButton, JButton stopButton, JLabel statusLabel)
	{
		super();
		this.customerType = customerType;
		this.customerQueueTextArea = customerQueueTextArea;
		this.startButton = startButton;
		this.stopButton = stopButton;
		this.statusLabel = statusLabel;
	}
	public void stop()
	{
		isRan = false;
	}
	public void start()
	{
		if(!isFirstRan )
		{
			isRan = true;
			backThread.interrupt();
		}
		else
			isFirstRan = false;
	}
	public boolean isRan()
	{
		return isRan;
	}
	@Override
	protected Void doInBackground() throws Exception
	{	
		backThread = Thread.currentThread();
		String comingCustomer = "";
		while (true)//not click cancle button
		{	
			if(isRan)
			{
				switch (this.customerType)
				{
				case Common:
					comingCustomer = generator.genCommonCustomer();
					break;
				case Fast:
					comingCustomer = generator.genFastCustomer();
					break;
				case VIP:
					comingCustomer = generator.genVIPCustomer();
					break;
				default:
					comingCustomer = "nobody";
				}
				//发布的过快,线程可能需要休息下,有待验证
				publish(comingCustomer);//publish the intermediate result to the process method as a list.
				
			}
			else
			{
				publish("sleeping\n");
				try
				{
					Thread.sleep(Long.MAX_VALUE);
				} catch (Exception e)
				{
					//不向外抛出异常
				}
			}
		}
		
	}//end doInBackground
	
	@Override
	protected void process(List<String> comingCustomers)
	{	
		//将发布的中间结果,显示到界面上
		for(String comingCustomer : comingCustomers)
			customerQueueTextArea.append(comingCustomer);
		
		super.process(comingCustomers);
	}//end process

	@Override
	protected void done()
	{	
		startButton.setEnabled(true);
		stopButton.setEnabled(false);
		
		//将计算的最终结果,显示在界面上
		try
		{
			this.get();
		} catch (InterruptedException e)
		{	
			statusLabel.setText("Interrupted while waiting for the results.");
			return;//要注意结束
		} catch (ExecutionException e)
		{
			statusLabel.setText("Error performing computation.");
			return;
		}catch(CancellationException e)//这个异常,是在调用lineUpworker.cancle(true)是产生的;
		{
			this.statusLabel.setText("canceled");
			return;
		}
		this.statusLabel.setText("over");
		super.done();
		
	}//end done
	
}//end class

public class ServiceWindowWorker extends SwingWorker<Void, String>
{	
	public static Integer ServedCount = 0;
	private final  ServiceWindow serviceWindow;
	private final JTextArea serviceInfoTextArea ; //display the service window's service info.
	private final JButton startButton ; //start window
	private final JButton stopButton ; //stop window
	private final JLabel statusLabel;
	private boolean isRan = true;
	private boolean isFirstRan = true;
	private Thread backThread = null;
	
	
	public ServiceWindowWorker(ServiceWindow serviceWindow,
			JTextArea customerQueueTextArea, JButton startButton,
			JButton stopButton, JLabel statusLabel)
	{
		super();
		this.serviceWindow = serviceWindow;
		this.serviceInfoTextArea = customerQueueTextArea;
		this.startButton = startButton;
		this.stopButton = stopButton;
		this.statusLabel = statusLabel;
	}
	public void stop()
	{
		isRan = false;
	}
	public void start()
	{	
		if(!isFirstRan)
		{
			isRan = true;
			backThread.interrupt();
		}
		else
			isFirstRan = false;
	}
	public boolean isRan()
	{
		return isRan;
	}
	@Override
	protected Void doInBackground() throws Exception
	{	
		backThread = Thread.currentThread();
		String publishText = null;//引用要在界面上显示的文字
		
		while(true)
		{	
			if(isRan)
			{
				publishText = this.serviceWindow.serve();
				ServedCount++;
				this.publish(publishText);// 发布中间结果到界面
			}
			else
			{
				this.publish("pause\n");
				synchronized (this)
				{
					try
					{
						this.wait();//没有synchronized没有等待,但编译不会报错
					} catch (Exception e)
					{
						//不向外抛出异常,就行
					}
				}
			}
		}
		
		
	}//end doInBackground
	
	@Override
	protected void process(List<String> chunks)
	{
		for(String publishText : chunks)
		{
			this.serviceInfoTextArea.append(publishText);
		}
		super.process(chunks);
		
	}//end process
	
	@Override
	protected void done()
	{	
		startButton.setEnabled(true);
		stopButton.setEnabled(false);
		try
		{
			this.get();
		} catch (InterruptedException e)
		{
			statusLabel.setText("Interrupted while waiting for results.");
			return;
		} catch (ExecutionException e)
		{	
			statusLabel.setText("Error computation");
			return;
		}catch(CancellationException e)
		{
			statusLabel.setText("canceled");
			return;
		}
		statusLabel.setText("over");//no think
		super.done();
		
	}//end done

}//end class

package cn.itcast.interview.bank.GUI;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import cn.itcast.interview.bank.GUI.panel.DisplayPanel;
import cn.itcast.interview.bank.GUI.panel.queue.CommonQueuePanel;
import cn.itcast.interview.bank.GUI.panel.queue.FastQueuePanel;
import cn.itcast.interview.bank.GUI.panel.queue.VIPQueuePanel;
import cn.itcast.interview.bank.GUI.panel.window.CommonWindowPanel;
import cn.itcast.interview.bank.GUI.panel.window.FastWindowPanel;
import cn.itcast.interview.bank.GUI.panel.window.VIPWindowPanel;
import cn.itcast.interview.bank.GUI.worker.ServiceWindowWorker;
import cn.itcast.interview.bank.business.LineUpMachine;

public class MainFrame extends JFrame
{
	private Map<String, DisplayPanel> panels = new HashMap<String, DisplayPanel>();
	private JButton startButton = new JButton("start");
	private JButton stopButton = new JButton("stop");
	private JLabel statusLabel = new JLabel("status:");
	
	public MainFrame()
	{
		this.setTitle("Bank Business Schedule System");

		this.setLayout(new BorderLayout());
		JLabel titleLabel = new JLabel("银行业务调度系统", JLabel.CENTER);
		titleLabel.setFont(new Font(Font.MONOSPACED, Font.BOLD, 20));
		this.add(titleLabel, BorderLayout.NORTH);
		
		JPanel panel_mid = new JPanel(new GridLayout(3, 1, 0, 0));
		
		JPanel panel_1 = new JPanel(new GridLayout(1, 3, 0, 0));
		JPanel panel_2 = new JPanel(new GridLayout(1, 4, 0, 0));
		JPanel panel_3 = new JPanel(new GridLayout(1, 2, 0, 0));
		
		JPanel panel_east = new JPanel();
		panel_east.setLayout(new BoxLayout(panel_east,BoxLayout.Y_AXIS));
		
		//队列面板
		CommonQueuePanel cq = new CommonQueuePanel("CommonQueue");
		panel_1.add(cq);
		panels.put("CommonQueue", cq);
		FastQueuePanel fq = new FastQueuePanel("FastQueue");
		panel_1.add(fq);
		panels.put("FastQueue", fq);
		VIPQueuePanel vq = new VIPQueuePanel("VIPQueue");
		panel_1.add(vq);
		panels.put("VIPQueue", vq);
		panel_mid.add(panel_1);
		
		//普通窗口面板
		String winName = "CommonWindow";
		for(int i=0; i<4; i++)
		{	
			CommonWindowPanel cw = new CommonWindowPanel(winName+i);
			panel_2.add(cw);
			panels.put(winName+i, cw);
		}
		panel_mid.add(panel_2);
		
		//快速窗口
		winName = "FastWindow";
		FastWindowPanel fw = new FastWindowPanel(winName);
		panel_3.add(fw);
		panels.put(winName, fw);
		//vip窗口
		winName = "VIPWindow";
		VIPWindowPanel vw = new VIPWindowPanel(winName);
		panel_3.add(vw);
		panels.put(winName, vw);
		panel_mid.add(panel_3);
		
		//添加中间区组件
		this.add(panel_mid, BorderLayout.CENTER);
		
		//启动按钮
		startButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		startButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{	
				startButton.setEnabled(false);
				stopButton.setEnabled(true);
				Collection<DisplayPanel> dps = panels.values();
				for(DisplayPanel dp : dps)
				{
					dp.startButton.doClick();
				}
			}
		});
		panel_east.add(startButton);
		//停止按钮
		stopButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		stopButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				startButton.setEnabled(true);
				stopButton.setEnabled(false);
				Collection<DisplayPanel> dps = panels.values();
				for(DisplayPanel dp : dps)
				{
					dp.stopButton.doClick();
				}
				
			}
		});
		panel_east.add(stopButton);
		//状态信息
		statusLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
		int comedCount = 
			LineUpMachine.CommonCustomerCount + LineUpMachine.FastCustomerCount + LineUpMachine.VIPCustomerCount;
		this.statusLabel.setText(comedCount+"/"+ServiceWindowWorker.ServedCount);
		panel_east.add(statusLabel);
		
		
		//添加东区组件
		this.add(panel_east, BorderLayout.EAST);
		
		//this.setSize(getMinimumSize());
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		this.setVisible(true);

	}
	
	public static void main(String[] args)
	{
		MainFrame main = new MainFrame();
		main.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
}
------- android培训 java培训 、期待与您交流!     ----------  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值