《电梯模拟程序.docx》由会员分享,可在线阅读,更多相关《电梯模拟程序.docx(44页珍藏版)》请在三一办公上搜索。
1、电梯模拟程序import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;public class LiftMainTestpublic static void main(String args)new LiftTest(电梯测试);/批量数据测试对话框class Dialog extends JDialog implements ActionListenerint Message=0;JPanel DJPanel=new JPanel;JTextField TestField=new JText
2、Field16;JLabel TestLabel1=new JLabel(时间);JLabel TestLabel2=new JLabel(人数);JLabel TestLabel3=new JLabel(起始楼层);JLabel TestLabel4=new JLabel(目标楼层);JButton ok=new JButton(确定);JButton cancel=new JButton(取消);Dialog(JFrame f,String s,boolean b)super(f,s,b);DJPanel.setLayout(new GridLayout(7,3);for(int i=0;
3、i<=15;i+)TestFieldi=new JTextField(4);DJPanel.add(TestLabel1);DJPanel.add(TestLabel2);DJPanel.add(TestLabel3);DJPanel.add(TestLabel4);DJPanel.add(TestField0);DJPanel.add(TestField1);DJPanel.add(TestField2);DJPanel.add(TestField3);DJPanel.add(TestField4);DJPanel.add(TestField5);DJPanel.add(TestFiel
4、d6);DJPanel.add(TestField7);DJPanel.add(TestField8);DJPanel.add(TestField9);DJPanel.add(TestField10);DJPanel.add(TestField11);DJPanel.add(TestField12);DJPanel.add(TestField13);DJPanel.add(TestField14);DJPanel.add(TestField15);DJPanel.add(ok);DJPanel.add(cancel);add(DJPanel);ok.addActionListener(this
5、);cancel.addActionListener(this);setBounds(450,170,300,300);public void actionPerformed(ActionEvent e)/Dialog监听函数if(e.getSource=ok)Message=1;setVisible(false);else if(e.getSource=cancel)Message=-1;setVisible(false);else;/LiftTest类class LiftTest extends JFrame implements ActionListener Dialog mydialo
6、g;JMenuBar menubar;JMenu menu,submenu,submenuhelp;JMenuItem item1,item2,menuitemhelp;/变量定义int x;int y;int LIsRun=0;/左电梯是否在运行int RIsRun=0;/右电梯是否在运行int LDirection=0;/左电梯运行方向int RDirection=0;/右电梯运行方向int LWantLevel=0;/乘客目的层数int RWantLevel=0;int LNowLevel=0;/乘客开始时所在层数int RNowLevel=0;int LLiftNowLevel=1;/
7、左电梯目前所在层数int RLiftNowLevel=1;/右电梯目前所在层数int WantDirection=0;/乘客目标运行方向int WaitPeopleNum=new int11;/保留数组int a=0;int b=0;/线程定义LIO lio=new LIO;RIO rio=new RIO;Thread LStartThread=new Thread(lio);Thread RStartThread=new Thread(rio);/上下电梯的按钮定义JButton Blue10=new JButton(new ImageIcon(10x.gif);JButton Blue9=
8、new JButton(new ImageIcon(9x.gif);JButton Blue8=new JButton(new ImageIcon(8x.gif);JButton Blue7=new JButton(new ImageIcon(7x.gif);JButton Blue6=new JButton(new ImageIcon(6x.gif);JButton Blue5=new JButton(new ImageIcon(5x.gif);JButton Blue4=new JButton(new ImageIcon(4x.gif);JButton Blue3=new JButton(
9、new ImageIcon(3x.gif);JButton Blue2=new JButton(new ImageIcon(2x.gif);JButton Red9=new JButton(new ImageIcon(9s.gif);JButton Red8=new JButton(new ImageIcon(8s.gif);JButton Red7=new JButton(new ImageIcon(7s.gif);JButton Red6=new JButton(new ImageIcon(6s.gif);JButton Red5=new JButton(new ImageIcon(5s.
10、gif);JButton Red4=new JButton(new ImageIcon(4s.gif);JButton Red3=new JButton(new ImageIcon(3s.gif);JButton Red2=new JButton(new ImageIcon(2s.gif);JButton Red1=new JButton(new ImageIcon(1s.gif);/电梯门定义JButton LLiftLDoor=new JButton(new ImageIcon(LLiftLDoor.gif);/设置门图标JButton LLiftRDoor=new JButton(new
11、 ImageIcon(LLiftRDoor.gif);/设置门图标JButton RLiftLDoor=new JButton(new ImageIcon(RLiftLDoor.gif);/设置门图标JButton RLiftRDoor=new JButton(new ImageIcon(RLiftRDoor.gif);/设置门图标/电梯内部按钮定义JButton LPressButton1=new JButton(1);JButton LPressButton2=new JButton(2);JButton LPressButton3=new JButton(3);JButton LPres
12、sButton4=new JButton(4);JButton LPressButton5=new JButton(5);JButton LPressButton6=new JButton(6);JButton LPressButton7=new JButton(7);JButton LPressButton8=new JButton(8);JButton LPressButton9=new JButton(9);JButton LPressButton10=new JButton(10);JButton RPressButton1=new JButton(1);JButton RPressB
13、utton2=new JButton(2);JButton RPressButton3=new JButton(3);JButton RPressButton4=new JButton(4);JButton RPressButton5=new JButton(5);JButton RPressButton6=new JButton(6);JButton RPressButton7=new JButton(7);JButton RPressButton8=new JButton(8);JButton RPressButton9=new JButton(9);JButton RPressButto
14、n10=new JButton(10);量数据测试”按钮的Panel/组件定义JLabel LL=new JLabel(当前电梯内部人数:);JLabel RL=new JLabel(当前电梯内部人数:);JButton Start=new JButton(开始电梯调度模拟);/开始电梯调度模拟按钮JButton Test=new JButton(开始批量数据测试);/开始批量数据测试按钮JTextArea LTextArea=new JTextArea(5,5);JTextArea RTextArea=new JTextArea(5,5);/电梯调度函数 Cal(LIsRun,LLiftNo
15、wLevel,q,LDirection)int Cal(int x,int y,int z,int w)return(x*(y-z)*w);/方法函数LiftTest(String s)super(s);setSize(1000,600);setLocation(100,50);/Dialogmydialog=new Dialog(this,批量数据测试,true);/菜单项设置menubar=new JMenuBar;menu=new JMenu(菜单);submenu=new JMenu(联系作者);submenuhelp=new JMenu(帮助);item1=new JMenuItem
16、(程序简介);item2=new JMenuItem(源代码);menu.add(item1);menu.addSeparator;menu.add(item2);menu.addSeparator;menu.add(submenu);menu.addSeparator;menu.add(submenuhelp);submenu.add(new JMenuItem(作者简介);submenuhelp.add(new JMenuItem(使用帮助);menubar.add(menu);setJMenuBar(menubar);/面板布局管理器设置WindowsPanel.setLayout(ne
17、w GridLayout(1,6);WaitPeopleNumPanel.setLayout(new GridLayout(10,1);LeftPanel.setLayout(new GridLayout(3,1);RightPanel.setLayout(new GridLayout(3,1);LeftLiftPanel.setLayout(null);/左电梯运动轨道Panel布局管理器设为空RightLiftPanel.setLayout(null);/右电梯运动轨道Panel布局管理器设为空UpDownButtonPanel.setLayout(new GridLayout(10,2)
18、;LButtonPanel.setLayout(new GridLayout(4,3);RButtonPanel.setLayout(new GridLayout(4,3);LeftLiftPanel.setBackground(Color.green);RightLiftPanel.setBackground(Color.green);UpDownButtonPanel.setBackground(Color.yellow);UpMainPanel.setLayout(new GridLayout(1,2);LBoxPanel.setLayout(null);/左电梯箱Panel布局管理器设
19、为空RBoxPanel.setLayout(null);/右电梯箱Panel布局管理器设为空/电梯箱设置LBoxPanel.setSize(165,51);RBoxPanel.setSize(165,51);LBoxPanel.setLocation(0,459);RBoxPanel.setLocation(0,459);LeftLiftPanel.add(LBoxPanel);RightLiftPanel.add(RBoxPanel);/电梯门设置LLiftLDoor.setEnabled(false);LLiftRDoor.setEnabled(false);RLiftLDoor.setE
20、nabled(false);RLiftRDoor.setEnabled(false);LLiftLDoor.setSize(82,51);LLiftRDoor.setSize(82,51);RLiftLDoor.setSize(82,51);RLiftRDoor.setSize(82,51);LLiftLDoor.setLocation(0,0);LLiftRDoor.setLocation(82,0);RLiftLDoor.setLocation(0,0);RLiftRDoor.setLocation(82,0);LBoxPanel.add(LLiftLDoor);LBoxPanel.add
21、(LLiftRDoor);RBoxPanel.add(RLiftLDoor);RBoxPanel.add(RLiftRDoor);/分别添加两个电梯的10个按钮LButtonPanel.add(LPressButton1);/左电梯LButtonPanel.add(LPressButton2);LButtonPanel.add(LPressButton3);LButtonPanel.add(LPressButton4);LButtonPanel.add(LPressButton5);LButtonPanel.add(LPressButton6);LButtonPanel.add(LPressB
22、utton7);LButtonPanel.add(LPressButton8);LButtonPanel.add(LPressButton9);LButtonPanel.add(LPressButton10); RButtonPanel.add(RPressButton1);/右电梯RButtonPanel.add(RPressButton2);RButtonPanel.add(RPressButton3);RButtonPanel.add(RPressButton4);RButtonPanel.add(RPressButton5);RButtonPanel.add(RPressButton6
23、);RButtonPanel.add(RPressButton7);RButtonPanel.add(RPressButton8);RButtonPanel.add(RPressButton9);RButtonPanel.add(RPressButton10);/添加上下电梯按钮至UpDownButtonPanel面板JButton Unabled1=new JButton;Unabled1.setEnabled(false);JButton Unabled2=new JButton;Unabled2.setEnabled(false);UpDownButtonPanel.add(Unable
24、d1);/开始第一个空着UpDownButtonPanel.add(Blue10);UpDownButtonPanel.add(Red9);UpDownButtonPanel.add(Blue9);UpDownButtonPanel.add(Red8);UpDownButtonPanel.add(Blue8);UpDownButtonPanel.add(Red7);UpDownButtonPanel.add(Blue7);UpDownButtonPanel.add(Red6);UpDownButtonPanel.add(Blue6);UpDownButtonPanel.add(Red5);Up
25、DownButtonPanel.add(Blue5);UpDownButtonPanel.add(Red4);UpDownButtonPanel.add(Blue4);UpDownButtonPanel.add(Red3);UpDownButtonPanel.add(Blue3);UpDownButtonPanel.add(Red2);UpDownButtonPanel.add(Blue2);UpDownButtonPanel.add(Red1);UpDownButtonPanel.add(Unabled2);/最后一个空着/等待人数文本框定义JTextField Text10=new JTe
26、xtField( 10楼等待人数:,8);Text10.setEditable(false);JTextField Text9=new JTextField( 9楼等待人数:,8);Text9.setEditable(false);JTextField Text8=new JTextField( 8楼等待人数:,8);Text8.setEditable(false);JTextField Text7=new JTextField( 7楼等待人数:,8);Text7.setEditable(false);JTextField Text6=new JTextField( 6楼等待人数:,8);Te
27、xt6.setEditable(false);JTextField Text5=new JTextField( 5楼等待人数:,8);Text5.setEditable(false);JTextField Text4=new JTextField( 4楼等待人数:,8);Text4.setEditable(false);JTextField Text3=new JTextField( 3楼等待人数:,8);Text3.setEditable(false);JTextField Text2=new JTextField( 2楼等待人数:,8);Text2.setEditable(false);J
28、TextField Text1=new JTextField( 1楼等待人数:,8);Text1.setEditable(false);/向UpMainPanel中添加组件UpMainPanel.add(Start);UpMainPanel.add(Test);/添加等待人数文本框至WaitPeopleNumPanel面板WaitPeopleNumPanel.add(Text10);WaitPeopleNumPanel.add(Text9);WaitPeopleNumPanel.add(Text8);WaitPeopleNumPanel.add(Text7);WaitPeopleNumPane
29、l.add(Text6);WaitPeopleNumPanel.add(Text5);WaitPeopleNumPanel.add(Text4);WaitPeopleNumPanel.add(Text3);WaitPeopleNumPanel.add(Text2);WaitPeopleNumPanel.add(Text1);/分别向LeftPanel(左电梯提示器),RightPanel(右电梯提示器)添加相应组件LeftPanel.add(LButtonPanel);LeftPanel.add(LL);LeftPanel.add(LTextArea);RightPanel.add(RButt
30、onPanel);RightPanel.add(RL);RightPanel.add(RTextArea);/添加至总面板WindowsPanel.add(WaitPeopleNumPanel);WindowsPanel.add(LeftPanel);WindowsPanel.add(LeftLiftPanel);WindowsPanel.add(UpDownButtonPanel);WindowsPanel.add(RightLiftPanel);WindowsPanel.add(RightPanel);/添加监听器Start.addActionListener(this);Test.add
31、ActionListener(this);Blue10.addActionListener(this);Blue9.addActionListener(this);Blue8.addActionListener(this);Blue7.addActionListener(this);Blue6.addActionListener(this);Blue5.addActionListener(this);Blue4.addActionListener(this);Blue3.addActionListener(this);Blue2.addActionListener(this);Red9.add
32、ActionListener(this);Red8.addActionListener(this);Red7.addActionListener(this);Red6.addActionListener(this);Red5.addActionListener(this);Red4.addActionListener(this);Red3.addActionListener(this);Red2.addActionListener(this);Red1.addActionListener(this);Monitor mon=new Monitor;/新建监听器对象LPressButton1.a
33、ddActionListener(mon);LPressButton2.addActionListener(mon);LPressButton3.addActionListener(mon);LPressButton4.addActionListener(mon);LPressButton5.addActionListener(mon);LPressButton6.addActionListener(mon);LPressButton7.addActionListener(mon);LPressButton8.addActionListener(mon);LPressButton9.addAc
34、tionListener(mon);LPressButton10.addActionListener(mon);RPressButton1.addActionListener(mon);RPressButton2.addActionListener(mon);RPressButton3.addActionListener(mon);RPressButton4.addActionListener(mon);RPressButton5.addActionListener(mon);RPressButton6.addActionListener(mon);RPressButton7.addActio
35、nListener(mon);RPressButton8.addActionListener(mon);RPressButton9.addActionListener(mon);RPressButton10.addActionListener(mon);/添加至窗口add(WindowsPanel,BorderLayout.CENTER);/主窗体添加至中间add(UpMainPanel,BorderLayout.NORTH);/UpMainPanel添加至北面setVisible(true);/Frame默认是不可见的,所有Panel都是默认可见的validate;/设置组件可见setRes
36、izable(false);/设置整个窗体是不可调整大小的setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/右上角“叉号”关闭按钮响应函数 /Runnable接口函数定义class LIO implements Runnable/左电梯接口函数public void runwhile(true)tryThread.sleep(5);/避免CPU使用率达到100%catch(InterruptedException a)while(LNowLevel!=0)int q=x;if(Cal(LIsRun,LLiftNowLevel,q,LDirectio
37、n)<=Cal(RIsRun,RLiftNowLevel,q,RDirection)/左电梯近LNowLevel=0;if(LLiftNowLevel<=q)for(int i=LLiftNowLevel;i<q;i+)LBoxPanel.setLocation(0,(459-i*51);LTextArea.append(妹妹n);RTextArea.append(哥哥n);LLiftNowLevel=i;LIsRun=1;LDirection=1;if(LNowLevel!=0&i=(LNowLevel-1)&LDirection=1&WantDirection=1)/停留等待4
38、秒for(int v=1;v<=82;v+)/开门tryThread.sleep(49);catch(InterruptedException a)LLiftLDoor.setLocation(-v),0);LLiftRDoor.setLocation(82+v),0);tryThread.sleep(4000);catch(InterruptedException a)for(int v=1;v<=82;v+)/关门tryThread.sleep(49);catch(InterruptedException a)LLiftLDoor.setLocation(v-82),0);LLif
39、tRDoor.setLocation(163-v),0);tryThread.sleep(1000);catch(InterruptedException a) elsefor(int i=LLiftNowLevel;i>q;i-)LBoxPanel.setLocation(0,(459-(i-2)*51);LTextArea.append(妹妹);RTextArea.append(哥哥);LLiftNowLevel=i;LIsRun=1;LDirection=-1;if(LNowLevel!=0&i=(LNowLevel+1)&LDirection=-1&WantDirection=-1
40、)/停留等待4秒for(int v=1;v<=82;v+)/开门tryThread.sleep(49);catch(InterruptedException a)LLiftLDoor.setLocation(-v),0);LLiftRDoor.setLocation(82+v),0);tryThread.sleep(4000);catch(InterruptedException a)for(int v=1;v<=82;v+)/关门tryThread.sleep(49);catch(InterruptedException a)LLiftLDoor.setLocation(v-82),
41、0);LLiftRDoor.setLocation(163-v),0); tryThread.sleep(1000);catch(InterruptedException a)LIsRun=0;LDirection=0;LLiftNowLevel=q; elsebreak;for(int e=1;e<=82;e+)/开门if(a=1)/识别用户按下电梯箱内小按钮x=LWantLevel;LNowLevel=LWantLevel;a=0;/a值复位tryThread.sleep(49);catch(InterruptedException a)LLiftLDoor.setLocation(-
42、e),0);LLiftRDoor.setLocation(82+e),0);for(int e=1;e<=82;e+)if(a=1)/识别用户按下电梯箱内小按钮x=LWantLevel;LNowLevel=LWantLevel;a=0;/a值复位tryThread.sleep(49);catch(InterruptedException a)for(int e=1;e<=82;e+)/关门if(a=1)/识别用户按下电梯箱内小按钮x=LWantLevel;LNowLevel=LWantLevel;a=0;/a值复位tryThread.sleep(49);catch(Interrupte
43、dException a)LLiftLDoor.setLocation(e-82),0);LLiftRDoor.setLocation(163-e),0); class RIO implements Runnable/右电梯接口函数public void runwhile(true)tryThread.sleep(5);catch(InterruptedException a)while(RNowLevel!=0)int p=y;if(Cal(LIsRun,LLiftNowLevel,p,LDirection)>=Cal(RIsRun,RLiftNowLevel,p,RDirection)
44、/右电梯近RNowLevel=0;if(RLiftNowLevel<=p)for(int i=RLiftNowLevel;i<p;i+)RBoxPanel.setLocation(0,(459-i*51);LTextArea.append(妹妹n);RTextArea.append(哥哥n);RLiftNowLevel=i;RIsRun=1;RDirection=1;if(RNowLevel!=0&i=(RNowLevel-1)&RDirection=1&WantDirection=1)/停留等待4秒for(int v=1;v<=82;v+)tryThread.sleep(49);catch(InterruptedException a)RLiftLDoor.setLocation(-v),0);RLiftRDoor.setLocation(82+v),0);tryThread.sleep(4000);catch(InterruptedException a)for(int v=1;v<=82;v+)tryThread.sleep(49);