电梯模拟程序(Java语言编写).doc_第1页
电梯模拟程序(Java语言编写).doc_第2页
电梯模拟程序(Java语言编写).doc_第3页
电梯模拟程序(Java语言编写).doc_第4页
电梯模拟程序(Java语言编写).doc_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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 jtextfi

2、eld16;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;i=

3、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(testfield6);d

4、jpanel.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);can

5、cel.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 mydialog

6、;jmenubar menubar;jmenu menu,submenu,submenuhelp;jmenuitem item1,item2,menuitemhelp;/变量定义int x;int y;int lisrun=0;/左电梯是否在运行(0或1)int risrun=0;/右电梯是否在运行(0或1)int ldirection=0;/左电梯运行方向int rdirection=0;/右电梯运行方向int lwantlevel=0;/乘客目的层数int rwantlevel=0;int lnowlevel=0;/乘客开始时所在层数int rnowlevel=0;int lliftnow

7、level=1;/左电梯目前所在层数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.gi

8、f);jbutton blue9=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 b

9、lue3=new jbutton(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(

10、new imageicon(5s.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 lliftrdoo

11、r=new jbutton(new 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

12、(3);jbutton lpressbutton4=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

13、);jbutton rpressbutton2=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);jb

14、utton rpressbutton10=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);/电梯调度函数 c

15、al(lisrun,lliftnowlevel,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(帮助);

16、item1=new jmenuitem(程序简介);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);/面板布局管理器设置

17、windowspanel.setlayout(new 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.setla

18、yout(new gridlayout(10,2);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.setlayo

19、ut(null);/左电梯箱panel布局管理器设为空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.setenabl

20、ed(false);rliftldoor.setenabled(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(

21、lliftldoor);lboxpanel.add(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)

22、;lbuttonpanel.add(lpressbutton7);lbuttonpanel.add(lpressbutton8);lbuttonpanel.add(lpressbutton9);lbuttonpanel.add(lpressbutton10); rbuttonpanel.add(rpressbutton1);/右电梯rbuttonpanel.add(rpressbutton2);rbuttonpanel.add(rpressbutton3);rbuttonpanel.add(rpressbutton4);rbuttonpanel.add(rpressbutton5);rbutt

23、onpanel.add(rpressbutton6);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)

24、;updownbuttonpanel.add(unabled1);/开始第一个空着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);u

25、pdownbuttonpanel.add(red5);updownbuttonpanel.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);/最后一个空着/等待人数文

26、本框定义jtextfield text10=new jtextfield( 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=

27、new jtextfield( 6楼等待人数:,8);text6.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楼等待人数:,

28、8);text2.seteditable(false);jtextfield 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

29、.add(text7);waitpeoplenumpanel.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(lt

30、extarea);rightpanel.add(rbuttonpanel);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.add

31、actionlistener(this);test.addactionlistener(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.add

32、actionlistener(this);red9.addactionlistener(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 mo

33、nitor();/新建监听器对象lpressbutton1.addactionlistener(mon);lpressbutton2.addactionlistener(mon);lpressbutton3.addactionlistener(mon);lpressbutton4.addactionlistener(mon);lpressbutton5.addactionlistener(mon);lpressbutton6.addactionlistener(mon);lpressbutton7.addactionlistener(mon);lpressbutton8.addactionli

34、stener(mon);lpressbutton9.addactionlistener(mon);lpressbutton10.addactionlistener(mon);rpressbutton1.addactionlistener(mon);rpressbutton2.addactionlistener(mon);rpressbutton3.addactionlistener(mon);rpressbutton4.addactionlistener(mon);rpressbutton5.addactionlistener(mon);rpressbutton6.addactionliste

35、ner(mon);rpressbutton7.addactionlistener(mon);rpressbutton8.addactionlistener(mon);rpressbutton9.addactionlistener(mon);rpressbutton10.addactionlistener(mon);/添加至窗口add(windowspanel,borderlayout.center);/主窗体添加至中间add(upmainpanel,borderlayout.north);/upmainpanel添加至北面setvisible(true);/frame默认是不可见的,所有pan

36、el都是默认可见的validate();/设置组件可见setresizable(false);/设置整个窗体是不可调整大小的setdefaultcloseoperation(jframe.exit_on_close);/右上角“叉号”关闭按钮响应函数 /runnable接口函数定义class lio implements runnable/左电梯接口函数public void run()while(true)trythread.sleep(5);/避免cpu使用率达到100%catch(interruptedexception a)while(lnowlevel!=0)int q=x; if(

37、cal(lisrun,lliftnowlevel,q,ldirection)=cal(risrun,rliftnowlevel,q,rdirection)/左电梯近lnowlevel=0;if(lliftnowlevel=q) for(int i=lliftnowlevel;iq;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)&

38、ldirection=1&wantdirection=1)/停留等待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;vq;i-) lboxpanel.setlocation(0,(459-(i-2)*51); ltextarea.ap

39、pend(妹妹); rtextarea.append(哥哥); lliftnowlevel=i; lisrun=1; ldirection=-1; if(lnowlevel!=0&i=(lnowlevel+1)&ldirection=-1&wantdirection=-1)/停留等待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(

40、4000); catch(interruptedexception a) for(int v=1;v=82;v+)/关门 trythread.sleep(49); catch(interruptedexception a) lliftldoor.setlocation(v-82),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+

41、)/开门if(a=1)/识别用户按下电梯箱内小按钮x=lwantlevel;lnowlevel=lwantlevel;a=0;/a值复位trythread.sleep(49);catch(interruptedexception a)lliftldoor.setlocation(-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(interrupte

42、dexception a)for(int e=1;e=cal(risrun,rliftnowlevel,p,rdirection)/右电梯近 rnowlevel=0;if(rliftnowlevel=p) for(int i=rliftnowlevel;ip;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=

43、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;vp;i-) rboxpanel.setlocation(0,(459-(i-2)*51); ltextarea.append(妹妹); rtex

44、tarea.append(哥哥); 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(inte

45、rruptedexception a) for(int v=1;v=82;v+) trythread.sleep(49); catch(interruptedexception a) rliftldoor.setlocation(v-82),0); rliftrdoor.setlocation(163-v),0); trythread.sleep(1000); catch(interruptedexception a) risrun=0;rdirection=0;rliftnowlevel=p; else break;for(int e=1;e=82;e+)if(b=1)/识别用户按下电梯箱内

46、小按钮y=rwantlevel;rnowlevel=rwantlevel;b=0;/b值复位trythread.sleep(49);catch(interruptedexception a)rliftldoor.setlocation(-e),0);rliftrdoor.setlocation(82+e),0);for(int e=1;e=82;e+)if(b=1)/识别用户按下电梯箱内小按钮y=rwantlevel;rnowlevel=rwantlevel;b=0;/b值复位trythread.sleep(49);catch(interruptedexception a)for(int e=

47、1;e=82;e+)if(b=1)/识别用户按下电梯箱内小按钮y=rwantlevel;rnowlevel=rwantlevel;b=0;/b值复位trythread.sleep(49);catch(interruptedexception a)rliftldoor.setlocation(e-82),0);rliftrdoor.setlocation(163-e),0);/开始按钮和上下键接口函数public void actionperformed(actionevent e)if(e.getsource()=start)lstartthread.start();rstartthread.start();else if(e.getsource()=test)mydialog.setvisible(true);else if(e.getsource()=red1)lnowlevel=1;rnowlevel=1;x=1;y=1;wantdirection=1;else if(e.getsource()=red2)lnowlevel=2;rnowlevel=2;x=2;y=2;wantdirection=1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论