电梯模拟程序(Java语言编写)
JAVA模拟电梯

1.import javax.swing.*;2.import java.awt.*;3.import java.util.*;4.public class Hufan extends JFrame{5.static HuPanel hp;6.public static void main(String[] args) {7.new Hufan();8.}9.public Hufan(){10.hp=new HuPanel();11.this.add(hp);12.this.setTitle("模拟电梯");13.this.setSize(350, 650);14.this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);15.this.setVisible(true);16.}17.}18.class HuPanel extends JPanel{19.DianTi dt;20.Person ps;21.public HuPanel(){22.init();23.dt=new DianTi(45, 0);24.//启动电梯线程25.Thread t=new Thread(dt);26.t.start();27.}28.public DianTi getDianTi(){29.return dt;30.}31.public Person getPerson(){32.return ps;33.}34.public void init(){35.Random r=new Random();36.int i=r.nextInt(4)+1;37.switch(i){38.case 1:39.ps=new Person(350,500,0,1);40.break;41.case 2:42.ps=new Person(350,350,0,2);43.break;44.case 3:45.ps=new Person(350,200,0,3);46.break;47.case 4:48.ps=new Person(350,50,0,4);49.break;50.}51.Thread t=new Thread(ps);52.t.start();53.}54.public void paint(Graphics g){55.super.paint(g);56.//画楼梯与楼层57.g.fillRect(30, 0, 6, 600);58.g.fillRect(200, 0, 6, 600);59.g.fillRect(200, 150, 330, 6);60.g.fillRect(200, 300, 330, 6);61.g.fillRect(200, 450, 330, 6);62.g.fillRect(30, 600, 500, 6);63.//画电梯64.g.setColor(Color.gray);65.g.fillRect(dt.x, dt.y, 150, 150);66.//画人67.g.setColor(Color.red);68.g.fillRect(ps.x, ps.y, 50, 100);69.//画要上几楼70.g.setColor(Color.black);71.g.setFont(new Font("宋体",Font.BOLD,17));72.if(ps.endFloor!=0&&ps.direct==0){73.g.drawString("去"+ps.endFloor+"楼", ps.x, ps.y+20);74.}75.//画我走了76.if(ps.direct==1){77.g.drawString("我走了", ps.x, ps.y+20);78.}79.}80.81.}82.class Person implements Runnable{83.int x;84.int y;85.int direct;86.int startFloor;87.int endFloor;88.int speed=5;89.boolean isStop=true;90.boolean isDead=true;91.DianTi dt;92.public Person(int x,int y,int direct,int startFloor){93.this.x=x;94.this.y=y;95.this.direct=direct;96.this.startFloor=startFloor;97.}98.//得到目的楼99.public void getEndF(){100.endFloor=new Random().nextInt(4)+1;101.}102.@Override103.public void run() {104.// TODO Auto-generated method stub105.//得到ps106.dt=Hufan.hp.dt;107.while(isDead){108.if(isStop){109.if(direct==0){110.x-=speed;111.if(x==200){112.while(true){113.this.getEndF();114.//如果目的楼和自己等,重新得到endFloor 115.if(startFloor==endFloor){116.this.getEndF();117.}else{118.break;119.}120.}121.//到了电梯,这时让电梯到自己在的楼层122.dt.topDown=startFloor;123.//启动电梯124.dt.isStop=true;125.//人停止126.isStop=false;127.}128.}else if(direct==1){129.x+=speed;130.//如果到边界。
电梯模拟程序课程设计

电梯模拟程序课程设计一、课程目标知识目标:1. 让学生理解电梯的工作原理,掌握电梯运行的模拟编程知识。
2. 使学生掌握利用流程图描述电梯运行逻辑的方法。
3. 帮助学生理解并运用条件语句和循环语句实现电梯模拟程序。
技能目标:1. 培养学生运用编程语言(如Scratch或Python)编写简单电梯模拟程序的能力。
2. 培养学生通过流程图分析问题、解决问题的能力。
3. 提高学生团队协作、沟通交流的能力。
情感态度价值观目标:1. 培养学生对编程的兴趣,激发学生主动探索新知识的热情。
2. 培养学生严谨、细心的学习态度,增强面对困难的勇气和毅力。
3. 培养学生遵守程序设计规范,养成良好的编程习惯。
课程性质:本课程为信息技术学科的课程,以项目式学习为主,注重实践操作和团队合作。
学生特点:学生为五年级学生,具备一定的信息技术基础,对新事物充满好奇,喜欢动手操作。
教学要求:教师需引导学生通过自主探究、小组合作等方式完成课程内容,注重培养学生的实践能力和创新精神。
同时,关注学生的个体差异,提供有针对性的指导。
通过本课程的学习,使学生能够达到上述课程目标,实现具体的学习成果。
二、教学内容1. 电梯工作原理介绍:包括电梯的基本结构、运行原理、信号控制系统等,让学生对电梯的运行机制有整体认识。
2. 编程语言基础:回顾并巩固Scratch或Python编程语言的基础知识,如变量、列表、条件语句、循环语句等,为编写电梯模拟程序打下基础。
3. 流程图设计:学习如何利用流程图描述电梯运行的逻辑过程,分析电梯运行中的各种情况,并进行流程图设计。
4. 电梯模拟程序编写:根据流程图,运用所学编程知识,分组合作编写电梯模拟程序,实现基本的运行、停止、开门、关门等功能。
5. 程序调试与优化:指导学生如何调试程序,发现并解决程序中的问题,提高程序的稳定性和运行效率。
教学内容安排和进度:第一课时:电梯工作原理介绍,回顾编程语言基础知识。
第二课时:学习流程图设计,分析电梯运行逻辑。
层电梯楼层组态王模拟程序

层电梯楼层组态王模拟程序/*电梯楼层指示*/if(一层门开关>0)//一层指示//{L1=1;}else{L1=0;} if(二层门开关>0)//二层指示//{L2=1;}else{L2=0;} if(三层门开关>0)//二层指示//{L3=1;}else{L3=0;} if(四层门开关>0)//二层指示//{L4=1;}else{L4=0;} /*电梯在一层时,四楼电梯外下呼*/if(D4==1&&J轿厢==0){一层门开关=一层门开关-10;DN4=1;}if(D4==1&&一层门开关==0&&SN1==0&&SN2==0&&SN3==0&&SN4==0) {J轿厢=J轿厢+2;}if(J轿厢>=102&&J轿厢<105){J轿厢=102;DN4=0;D4=0;}if(J轿厢==102&&SN1==0&&SN2==0&&SN3==0&&SN4==0) {if(四层门开关<60)四层门开关=四层门开关+10;} /*电梯在一层时,三楼电梯外下呼*/ if(D3==1&&J轿厢==0){一层门开关=一层门开关-10;DN3=1;}if(D3==1&&一层门开关==0){J轿厢=J轿厢+2;}if(D3==1&&J轿厢>=68&&J轿厢<74){J轿厢=68;DN3=0;if(三层门开关<60)三层门开关=三层门开关+10;}?/*电梯在一层时,二楼电梯外下呼*/if(D2==1&&J轿厢==0){一层门开关=一层门开关-10;DN2=1;}if(D2==1&&一层门开关==0){J轿厢=J轿厢+2;}if(D2==1&&J轿厢>=34&&J轿厢<40){J轿厢=34;DN2=0;if(二层门开关<60)二层门开关=二层门开关+10;}?/*电梯内呼*/if(J轿厢==102&&SN1==1)//电梯在四楼时,内呼一层//{四层门开关=四层门开关-10;}if(四层门开关==0&&SN1==1){J轿厢=J轿厢-2;}if(J轿厢==0&&D2==0&&D3==0&&D4==0&&U1==0&&U2==0&&U 3==0){if(一层门开关<60)一层门开关=一层门开关+10;SN1=0;}?if(J轿厢==102&&SN2==1)//电梯在四楼时,内呼二层//{四层门开关=四层门开关-10;}if(四层门开关==0&&SN2==1){J轿厢=J轿厢-2;}if(SN2==1&&J轿厢<=34&&J轿厢>30){J轿厢=34;}if(J轿厢==34&&D2==0&&D3==0&&D4==0&&U1==0&&U2==0&& U3==0&&SN1==0){if(二层门开关<60)二层门开关=二层门开关+10;SN2=0;}?if(J轿厢==102&&SN3==1)//电梯在四楼时,内呼三层//{四层门开关=四层门开关-10;}if(四层门开关==0&&SN3==1){J轿厢=J轿厢-2;}if(SN3==1&&J轿厢<=68&&J轿厢>64){J轿厢=68;}if(J轿厢==68&&D2==0&&D3==0&&D4==0&&U1==0&&U2==0&& U3==0&&SN2==0&&SN1==0){if(三层门开关<60)三层门开关=三层门开关+10;SN3=0;} /*电梯上升信号*/if(J轿厢==0&&(D2==1||D3==1||D4==1||U2==1||U3==1||SN2==1||SN3 ==1||SN4==1))//电梯在一层时的上升信号//{UP=1;DOWN=0;}if(J轿厢==34&&(D3==1||D4==1||U3==1||SN3==1||SN4==1))//电梯在二层时的上升信号// {UP=1;DOWN=0;}if(J轿厢==68&&(D4==1||SN4==1))//电梯在三层时的上升信号//{UP=1;DOWN=0;} /*电梯下降信号*/if(J轿厢==102&&(D2==1||D3==1||U1==1||U2==1||U3==1||SN1==1||S N2==1||SN3==1))//电梯在四层时的下降信号//{DOWN=1;UP=0;}if(J轿厢==68&&(D2==1||U1==1||U2==1||SN1==1||SN2==1))//电梯在三层时的下降信号// {DOWN=1;UP=0;}if(J轿厢==34&&U1==1||SN1==1)//电梯在二层时的下降信号//{DOWN=1;UP=0;}。
电梯模拟程序(Java语言编写)

import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;public class LiftMainTest{public static void main(String args[]){new LiftTest("电梯测试");}}//批量数据测试对话框class Dialog extends JDialog implements ActionListener {int Message=0;JPanel DJPanel=new JPanel();JTextField TestField[]=new JTextField[16];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<=15;i++){TestField[i]=new JTextField(4);}DJPanel.add(TestLabel1);DJPanel.add(TestLabel2);DJPanel.add(TestLabel3);DJPanel.add(TestLabel4);DJPanel.add(TestField[0]);DJPanel.add(TestField[1]);DJPanel.add(TestField[2]);DJPanel.add(TestField[3]);DJPanel.add(TestField[4]);DJPanel.add(TestField[5]);DJPanel.add(TestField[6]);DJPanel.add(TestField[7]);DJPanel.add(TestField[8]);DJPanel.add(TestField[9]);DJPanel.add(TestField[10]);DJPanel.add(TestField[11]);DJPanel.add(TestField[12]);DJPanel.add(TestField[13]);DJPanel.add(TestField[14]);DJPanel.add(TestField[15]);DJPanel.add(ok);DJPanel.add(cancel);add(DJPanel);ok.addActionListener(this);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 mydialog;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 LLiftNowLevel=1;//左电梯目前所在层数int RLiftNowLevel=1;//右电梯目前所在层数int WantDirection=0;//乘客目标运行方向int WaitPeopleNum[]=new int[11];//保留数组(未用)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=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(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.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 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 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");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");JButton 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);//电梯调度函数Cal(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("帮助");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);//面板布局管理器设置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.setLayout(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.setLayout(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.setEnabled(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(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); 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); RButtonPanel.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); 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); UpDownButtonPanel.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);//最后一个空着//等待人数文本框定义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=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楼等待人数:",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.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(LTextArea);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.addActionListener(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.addActionListener(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 Monitor();//新建监听器对象LPressButton1.addActionListener(mon);LPressButton2.addActionListener(mon);LPressButton3.addActionListener(mon);LPressButton4.addActionListener(mon);LPressButton5.addActionListener(mon);LPressButton6.addActionListener(mon);LPressButton7.addActionListener(mon);LPressButton8.addActionListener(mon);LPressButton9.addActionListener(mon);LPressButton10.addActionListener(mon);RPressButton1.addActionListener(mon);RPressButton2.addActionListener(mon);RPressButton3.addActionListener(mon);RPressButton4.addActionListener(mon);RPressButton5.addActionListener(mon);RPressButton6.addActionListener(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默认是不可见的,所有Panel都是默认可见的validate();//设置组件可见setResizable(false);//设置整个窗体是不可调整大小的setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//右上角“叉号”关闭按钮响应函数}//Runnable接口函数定义class LIO implements Runnable//左电梯接口函数{public void run(){while(true){try{Thread.sleep(5);}//避免CPU使用率达到100%catch(InterruptedException a){}while(LNowLevel!=0){int q=x;if(Cal(LIsRun,LLiftNowLevel,q,LDirection)<=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&&WantDirecti on==1)//停留等待4秒{for(int v=1;v<=82;v++)//开门{try{Thread.sleep(49);}catch(InterruptedException a){}LLiftLDoor.setLocation((-v),0);LLiftRDoor.setLocation((82+v),0);}try{Thread.sleep(4000);}catch(InterruptedException a){}for(int v=1;v<=82;v++)//关门{try{Thread.sleep(49);}catch(InterruptedException a){}LLiftLDoor.setLocation((v-82),0);LLiftRDoor.setLocation((163-v),0);}}try{Thread.sleep(1000);}catch(InterruptedException a){}}}else{for(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)//停留等待4秒{for(int v=1;v<=82;v++)//开门{try{Thread.sleep(49);}catch(InterruptedException a){}LLiftLDoor.setLocation((-v),0);LLiftRDoor.setLocation((82+v),0);}try{Thread.sleep(4000);}catch(InterruptedException a){}for(int v=1;v<=82;v++)//关门{try{Thread.sleep(49);}catch(InterruptedException a){}LLiftLDoor.setLocation((v-82),0);LLiftRDoor.setLocation((163-v),0);}}try{Thread.sleep(1000);}catch(InterruptedException a){}}}LIsRun=0;LDirection=0;LLiftNowLevel=q;}else{break;}for(int e=1;e<=82;e++)//开门{if(a==1)//识别用户按下电梯箱内小按钮{x=LWantLevel;LNowLevel=LWantLevel;a=0;//"a"值复位}try{Thread.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"值复位}try{Thread.sleep(49);}catch(InterruptedException a){}}for(int e=1;e<=82;e++)//关门{if(a==1)//识别用户按下电梯箱内小按钮{x=LWantLevel;LNowLevel=LWantLevel;a=0;//"a"值复位}try{Thread.sleep(49);}catch(InterruptedException a){}LLiftLDoor.setLocation((e-82),0);LLiftRDoor.setLocation((163-e),0);}}}}}class RIO implements Runnable//右电梯接口函数{public void run(){while(true){try{Thread.sleep(5);}catch(InterruptedException a){}while(RNowLevel!=0){int p=y;if(Cal(LIsRun,LLiftNowLevel,p,LDirection)>=Cal(RIsRun,RLiftNowLevel,p,RDir ection))//右电梯近{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&&WantDirec tion==1)//停留等待4秒{for(int v=1;v<=82;v++){try{Thread.sleep(49);}catch(InterruptedException a){}RLiftLDoor.setLocation((-v),0);RLiftRDoor.setLocation((82+v),0);}try{Thread.sleep(4000);}catch(InterruptedException a){}for(int v=1;v<=82;v++){try{Thread.sleep(49);}catch(InterruptedException a){}RLiftLDoor.setLocation((v-82),0);RLiftRDoor.setLocation((163-v),0);}}try{Thread.sleep(1000);}catch(InterruptedException a){}}}else{for(int i=RLiftNowLevel;i>p;i--){RBoxPanel.setLocation(0,(459-(i-2)*51));LTextArea.append("妹妹");RTextArea.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++){try{Thread.sleep(49);}catch(InterruptedException a){}RLiftLDoor.setLocation((-v),0);RLiftRDoor.setLocation((82+v),0);}try{Thread.sleep(4000);}catch(InterruptedException a){}for(int v=1;v<=82;v++){try{Thread.sleep(49);}catch(InterruptedException a){}RLiftLDoor.setLocation((v-82),0);RLiftRDoor.setLocation((163-v),0);}}try{Thread.sleep(1000);}catch(InterruptedException a){}}}RIsRun=0;RDirection=0;RLiftNowLevel=p;}else{break;}for(int e=1;e<=82;e++){if(b==1)//识别用户按下电梯箱内小按钮{y=RWantLevel;RNowLevel=RWantLevel;b=0;//"b"值复位}try{Thread.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"值复位}try{Thread.sleep(49);}catch(InterruptedException a){}}for(int e=1;e<=82;e++){if(b==1)//识别用户按下电梯箱内小按钮{y=RWantLevel;RNowLevel=RWantLevel;b=0;//"b"值复位}try{Thread.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;}else if(e.getSource()==Red3){LNowLevel=3;RNowLevel=3;x=3;y=3;WantDirection=1;}else if(e.getSource()==Red4){LNowLevel=4;RNowLevel=4;x=4;y=4;WantDirection=1;}else if(e.getSource()==Red5){LNowLevel=5;RNowLevel=5;x=5;y=5;WantDirection=1;}else if(e.getSource()==Red6){LNowLevel=6;RNowLevel=6;x=6;y=6;WantDirection=1;}else if(e.getSource()==Red7){LNowLevel=7;RNowLevel=7;x=7;y=7;WantDirection=1;}else if(e.getSource()==Red8){LNowLevel=8;RNowLevel=8;x=8;y=8;WantDirection=1;}else if(e.getSource()==Red9){LNowLevel=9;RNowLevel=9;x=9;y=9;WantDirection=1;}else if(e.getSource()==Blue10){LNowLevel=10;RNowLevel=10;x=10;y=10;WantDirection=-1; }else if(e.getSource()==Blue9){LNowLevel=9;RNowLevel=9;x=9;y=9;WantDirection=-1;}else if(e.getSource()==Blue8){LNowLevel=8;RNowLevel=8;x=8;y=8;WantDirection=-1;}else if(e.getSource()==Blue7)LNowLevel=7;RNowLevel=7;x=7;y=7;WantDirection=-1; }else if(e.getSource()==Blue6){LNowLevel=6;RNowLevel=6;x=6;y=6;WantDirection=-1; }else if(e.getSource()==Blue5){LNowLevel=5;RNowLevel=5;x=5;y=5;WantDirection=-1; }else if(e.getSource()==Blue4){LNowLevel=4;RNowLevel=4;x=4;y=4;WantDirection=-1; }else if(e.getSource()==Blue3){LNowLevel=3;RNowLevel=3;x=3;y=3;WantDirection=-1; }else if(e.getSource()==Blue2){LNowLevel=2;RNowLevel=2;x=2;y=2;WantDirection=-1; }else{;}}//左右电梯电梯箱内小按钮监听器class Monitor implements ActionListener{public void actionPerformed(ActionEvent e){if(e.getSource()==LPressButton1){LWantLevel=1;a=1;}else if(e.getSource()==LPressButton2){LWantLevel=2;a=1;}else if(e.getSource()==LPressButton3){LWantLevel=3;a=1;else if(e.getSource()==LPressButton4) {LWantLevel=4;a=1;}else if(e.getSource()==LPressButton5) {LWantLevel=5;a=1;}else if(e.getSource()==LPressButton6) {LWantLevel=6;a=1;}else if(e.getSource()==LPressButton7) {LWantLevel=7;a=1;}else if(e.getSource()==LPressButton8) {LWantLevel=8;a=1;}else if(e.getSource()==LPressButton9) {LWantLevel=9;a=1;}else if(e.getSource()==LPressButton10) {LWantLevel=10;a=1;}else if(e.getSource()==RPressButton1) {RWantLevel=1;b=1;}else if(e.getSource()==RPressButton2) {RWantLevel=2;b=1;}else if(e.getSource()==RPressButton3) {RWantLevel=3;b=1;}else if(e.getSource()==RPressButton4) {RWantLevel=4;b=1;else if(e.getSource()==RPressButton5){RWantLevel=5;b=1;}else if(e.getSource()==RPressButton6){RWantLevel=6;b=1;}else if(e.getSource()==RPressButton7){RWantLevel=7;b=1;}else if(e.getSource()==RPressButton8){RWantLevel=8;b=1;}else if(e.getSource()==RPressButton9){RWantLevel=9;b=1;}else if(e.getSource()==RPressButton10){RWantLevel=10;b=1;}else{;}}}}/*//////////////////////////////////////////////////////////////////////////////////文件说明:程序用java语言编写,自行安装JDK(下载网址:)。
电梯控制系统(Java课程设计)内容

(1)新的请求插入到上行队列头。
(2)电梯到达某个楼层将这个楼层的请求从上行队列的队列头删除,后继的更高楼层号成为对列头。
当电梯正在下行时,如果下行队列的队列头改变时,系统将同时将这个新的队列头发送给电梯作为它的新目的地。有两种原因引起下行队列头的改变。
(2)M被插入到上行队列的合适位置。
(3)如果M被插到上行队列头。
如果电梯正处于上行状态。将M发送给电梯作为目的地。
如果电梯处于停止状态。将M发送给电梯作为目的地。
(4)电梯到达此楼层。
(5)电梯自动打开门(用例9)。
(6)乘客进入电梯。
(7)超重测试(用例6)。
用例2按下某楼层的下行按钮(go down stair)。
用例8打开某层电梯锁(open floor)
此层的楼层请求按钮和电梯内的楼层按钮恢复正常。
用例9自动打开电梯门(auto open door)
(1)电梯到达某楼层。
(2)电梯自动开门。
用例10自动关闭电梯门(auto close door)
(1)电梯门打开的状态达到设置的时限。
(2)关闭电梯门。
A3 类模型
根据分析,我们可以得到下面这些类。
乘客类(Passenger)、管理员类(Manager)、电梯类(Elevator)、电梯门类(ElevatorDoor)、楼层的上行请求按钮类(Go Up Request)、楼层的下请求按钮类(Gequest)、楼层锁类(lock)、指示灯类(Indicate Light)、队列类(Queue)、定时器类(Timer)、异常处理类(Expection)。
电梯模拟系统

4 电梯系统的演示
查看
(1) 电 梯 电 路 模 型 设 计
电梯电路模型设计说明
1.电路模型说明 :
采用了两部电梯,分别用左右两排黄灯表示,黄灯亮表示当前电 梯位置,中间蓝灯亮表示对应楼层有人等待(蓝色灯为随即点亮)。
2.实际运行效果为:
按照前面规定的调度规则,去响应请求,模型的效果为依次点亮 电梯经过处相应的LED灯,时间间隔为3S,然后到达目标楼层,持 续点亮当前楼层对应的灯,表示电梯停止等待,随即灭掉所在楼层 对应的蓝色LED灯,表示人进入电梯。然后电梯继续运行到目标楼 层,持续点亮当前楼层对应的灯。表示电梯停止,再点亮对应楼层 的蓝色LED,蓝色灯亮2S,然后灭掉,表示人离开电梯。若其他楼 层没有请求,则电梯停在当前位置,表示为该楼层对应的LED所代 表的电梯一直亮,直到再去响应请求。
turn =TRUE;
..................................................................
相关代码实例如下:
else if(point[0].y==point[goFloor].y&&m_arrive==FALSE) {//m_arrive表示人员是否到达目标楼层 FALSE表示到达
说明:模拟电梯采用MFC界面设计。
2 电梯模拟系统需求分析
1.电梯的初始状态是电梯位于第一层处,所有按钮都没有按下。
2.乘客可以在任意时刻按任何一个目标钮和呼叫钮。呼叫和目标 对应的楼层可能不是电梯当前运行方向可达的楼层。(每个楼层出现的 乘客数量,以及所要到达的目标楼层的方向是随机产生的)
3.电梯系统依照某种预先定义好的调度策略对随机出现的呼叫和目 标进行分析和响应。
java电梯仿真(java实验设计)

用面向对象方法和面向对象程序设计语言,实现满足下述要求的一个高层建筑电梯活动仿真程序。
问题域概述某国际展览中心共40 层,设有载客电梯10 部(用E0~E9 标识)。
限定条件(1)电梯的运行规则是:E0、E1:可到达每层。
E2、E3:可到达1、25~40 层。
E4、E5:可到达1~25 层。
E6、E7:可到达1、2~40 层中的偶数层。
E8、E9:可到达1~39 层中的奇数层。
(2)每部电梯的最大乘员量均为K 人(K 值可以根据仿真情况在10~18 人之间确定)。
(3)仿真开始时,各电梯随机地处于其符合运行规则的任意一层,为空梯。
(4)仿真开始后,有N 人(0<N<1000)在M 分钟(0<M<10)内随机地到达该国际展览中心的1 层,开始乘梯活动。
(5)每位乘客初次所要到达的楼层是随机的,令其在合适的电梯处等待电梯到来。
(6)每位乘客乘坐合适的电梯到达指定楼层后,随机地停留10-120 秒后,再随机地去往另一楼层,依此类推,当每人乘坐过L 次(每人的L 值不同,在产生乘客时随机地在1~10 次之间确定)电梯后,第L+1 次为下至底层并结束乘梯行为。
到所有乘客结束乘梯行为时,本次仿真结束。
(7)电梯运行速度为S 秒/层(S 值可以根据仿真情况在1~5 之间确定),每人上下时间为T 秒(T 值可以根据仿真情况在2~10 之间确定)。
(8)电梯运行的方向由先发出请求者决定,不允许后发出请求者改变电梯的当前运行方向,除非是未被请求的空梯。
(9)当某层有乘客按下乘梯电钮时,优先考虑离该层最近的满足条件(8)能够最快到达目标层的电梯。
(10)不允许电梯超员。
开发结果的行为特征(1)产生事件的周期为1 秒,每次可产生0 个或多个事件。
(2)各随机事件由互不相关的伪随机数发生器决定。
(3)设计一个易于理解的界面,动态显示各梯的载客与运行情况,动态显示各楼层的人员停留情况与要求乘梯情况;动态显示从仿真开始到目前的时间。
Lifta.java

but.addActionListener(new UpListener(i));
but.setBackground(Color.gray);
controlBrick.add(but);
pan.add(but);
//向下键
upSignalTable[i] = 0;
downSignalTable[i] = 0;
}
//********************画图************************
for(int i=10;i>0;i--){
//最左边黑色列
controlTable = new int[10][2];
for (int i = 0; i < 10; i++)
for (int j = 0; j < 2; j++)
controlTable[i][j] [10];
public Brick(){
sign.setBounds(0, 0, 80, 40); //起始位置
//开关门部分
openclose.setBounds(80, 0, 80, 40);
openclose.setBackground(Color.yellow);
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;public class LiftMainTest{public static void main(String args[]){new LiftTest("电梯测试");}}//批量数据测试对话框class Dialog extends JDialog implements ActionListener {int Message=0;JPanel DJPanel=new JPanel();JTextField TestField[]=new JTextField[16];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<=15;i++){TestField[i]=new JTextField(4);}DJPanel.add(TestLabel1);DJPanel.add(TestLabel2);DJPanel.add(TestLabel3);DJPanel.add(TestLabel4);DJPanel.add(TestField[0]);DJPanel.add(TestField[1]);DJPanel.add(TestField[2]);DJPanel.add(TestField[3]);DJPanel.add(TestField[4]);DJPanel.add(TestField[5]);DJPanel.add(TestField[6]);DJPanel.add(TestField[7]);DJPanel.add(TestField[8]);DJPanel.add(TestField[9]);DJPanel.add(TestField[10]);DJPanel.add(TestField[11]);DJPanel.add(TestField[12]);DJPanel.add(TestField[13]);DJPanel.add(TestField[14]);DJPanel.add(TestField[15]);DJPanel.add(ok);DJPanel.add(cancel);add(DJPanel);ok.addActionListener(this);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 mydialog;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 LLiftNowLevel=1;//左电梯目前所在层数int RLiftNowLevel=1;//右电梯目前所在层数int WantDirection=0;//乘客目标运行方向int WaitPeopleNum[]=new int[11];//保留数组(未用)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=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(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.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 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 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");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");JButton 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);//电梯调度函数Cal(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("帮助");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);//面板布局管理器设置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.setLayout(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.setLayout(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.setEnabled(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(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); 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); RButtonPanel.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); 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); UpDownButtonPanel.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);//最后一个空着//等待人数文本框定义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=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楼等待人数:",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.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(LTextArea);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.addActionListener(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.addActionListener(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 Monitor();//新建监听器对象LPressButton1.addActionListener(mon);LPressButton2.addActionListener(mon);LPressButton3.addActionListener(mon);LPressButton4.addActionListener(mon);LPressButton5.addActionListener(mon);LPressButton6.addActionListener(mon);LPressButton7.addActionListener(mon);LPressButton8.addActionListener(mon);LPressButton9.addActionListener(mon);LPressButton10.addActionListener(mon);RPressButton1.addActionListener(mon);RPressButton2.addActionListener(mon);RPressButton3.addActionListener(mon);RPressButton4.addActionListener(mon);RPressButton5.addActionListener(mon);RPressButton6.addActionListener(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默认是不可见的,所有Panel都是默认可见的validate();//设置组件可见setResizable(false);//设置整个窗体是不可调整大小的setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//右上角“叉号”关闭按钮响应函数}//Runnable接口函数定义class LIO implements Runnable//左电梯接口函数{public void run(){while(true){try{Thread.sleep(5);}//避免CPU使用率达到100%catch(InterruptedException a){}while(LNowLevel!=0){int q=x;if(Cal(LIsRun,LLiftNowLevel,q,LDirection)<=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&&WantDirecti on==1)//停留等待4秒{for(int v=1;v<=82;v++)//开门{try{Thread.sleep(49);}catch(InterruptedException a){}LLiftLDoor.setLocation((-v),0);LLiftRDoor.setLocation((82+v),0);}try{Thread.sleep(4000);}catch(InterruptedException a){}for(int v=1;v<=82;v++)//关门{try{Thread.sleep(49);}catch(InterruptedException a){}LLiftLDoor.setLocation((v-82),0);LLiftRDoor.setLocation((163-v),0);}}try{Thread.sleep(1000);}catch(InterruptedException a){}}}else{for(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)//停留等待4秒{for(int v=1;v<=82;v++)//开门{try{Thread.sleep(49);}catch(InterruptedException a){}LLiftLDoor.setLocation((-v),0);LLiftRDoor.setLocation((82+v),0);}try{Thread.sleep(4000);}catch(InterruptedException a){}for(int v=1;v<=82;v++)//关门{try{Thread.sleep(49);}catch(InterruptedException a){}LLiftLDoor.setLocation((v-82),0);LLiftRDoor.setLocation((163-v),0);}}try{Thread.sleep(1000);}catch(InterruptedException a){}}}LIsRun=0;LDirection=0;LLiftNowLevel=q;}else{break;}for(int e=1;e<=82;e++)//开门{if(a==1)//识别用户按下电梯箱内小按钮{x=LWantLevel;LNowLevel=LWantLevel;a=0;//"a"值复位}try{Thread.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"值复位}try{Thread.sleep(49);}catch(InterruptedException a){}}for(int e=1;e<=82;e++)//关门{if(a==1)//识别用户按下电梯箱内小按钮{x=LWantLevel;LNowLevel=LWantLevel;a=0;//"a"值复位}try{Thread.sleep(49);}catch(InterruptedException a){}LLiftLDoor.setLocation((e-82),0);LLiftRDoor.setLocation((163-e),0);}}}}}class RIO implements Runnable//右电梯接口函数{public void run(){while(true){try{Thread.sleep(5);}catch(InterruptedException a){}while(RNowLevel!=0){int p=y;if(Cal(LIsRun,LLiftNowLevel,p,LDirection)>=Cal(RIsRun,RLiftNowLevel,p,RDir ection))//右电梯近{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&&WantDirec tion==1)//停留等待4秒{for(int v=1;v<=82;v++){try{Thread.sleep(49);}catch(InterruptedException a){}RLiftLDoor.setLocation((-v),0);RLiftRDoor.setLocation((82+v),0);}try{Thread.sleep(4000);}catch(InterruptedException a){}for(int v=1;v<=82;v++){try{Thread.sleep(49);}catch(InterruptedException a){}RLiftLDoor.setLocation((v-82),0);RLiftRDoor.setLocation((163-v),0);}}try{Thread.sleep(1000);}catch(InterruptedException a){}}}else{for(int i=RLiftNowLevel;i>p;i--){RBoxPanel.setLocation(0,(459-(i-2)*51));LTextArea.append("妹妹");RTextArea.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++){try{Thread.sleep(49);}catch(InterruptedException a){}RLiftLDoor.setLocation((-v),0);RLiftRDoor.setLocation((82+v),0);}try{Thread.sleep(4000);}catch(InterruptedException a){}for(int v=1;v<=82;v++){try{Thread.sleep(49);}catch(InterruptedException a){}RLiftLDoor.setLocation((v-82),0);RLiftRDoor.setLocation((163-v),0);}}try{Thread.sleep(1000);}catch(InterruptedException a){}}}RIsRun=0;RDirection=0;RLiftNowLevel=p;}else{break;}for(int e=1;e<=82;e++){if(b==1)//识别用户按下电梯箱内小按钮{y=RWantLevel;RNowLevel=RWantLevel;b=0;//"b"值复位}try{Thread.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"值复位}try{Thread.sleep(49);}catch(InterruptedException a){}}for(int e=1;e<=82;e++){if(b==1)//识别用户按下电梯箱内小按钮{y=RWantLevel;RNowLevel=RWantLevel;b=0;//"b"值复位}try{Thread.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;}else if(e.getSource()==Red3){LNowLevel=3;RNowLevel=3;x=3;y=3;WantDirection=1;}else if(e.getSource()==Red4){LNowLevel=4;RNowLevel=4;x=4;y=4;WantDirection=1;}else if(e.getSource()==Red5){LNowLevel=5;RNowLevel=5;x=5;y=5;WantDirection=1;}else if(e.getSource()==Red6){LNowLevel=6;RNowLevel=6;x=6;y=6;WantDirection=1;}else if(e.getSource()==Red7){LNowLevel=7;RNowLevel=7;x=7;y=7;WantDirection=1;}else if(e.getSource()==Red8){LNowLevel=8;RNowLevel=8;x=8;y=8;WantDirection=1;}else if(e.getSource()==Red9){LNowLevel=9;RNowLevel=9;x=9;y=9;WantDirection=1;}else if(e.getSource()==Blue10){LNowLevel=10;RNowLevel=10;x=10;y=10;WantDirection=-1; }else if(e.getSource()==Blue9){LNowLevel=9;RNowLevel=9;x=9;y=9;WantDirection=-1;}else if(e.getSource()==Blue8){LNowLevel=8;RNowLevel=8;x=8;y=8;WantDirection=-1;}else if(e.getSource()==Blue7)LNowLevel=7;RNowLevel=7;x=7;y=7;WantDirection=-1; }else if(e.getSource()==Blue6){LNowLevel=6;RNowLevel=6;x=6;y=6;WantDirection=-1; }else if(e.getSource()==Blue5){LNowLevel=5;RNowLevel=5;x=5;y=5;WantDirection=-1; }else if(e.getSource()==Blue4){LNowLevel=4;RNowLevel=4;x=4;y=4;WantDirection=-1; }else if(e.getSource()==Blue3){LNowLevel=3;RNowLevel=3;x=3;y=3;WantDirection=-1; }else if(e.getSource()==Blue2){LNowLevel=2;RNowLevel=2;x=2;y=2;WantDirection=-1; }else{;}}//左右电梯电梯箱内小按钮监听器class Monitor implements ActionListener{public void actionPerformed(ActionEvent e){if(e.getSource()==LPressButton1){LWantLevel=1;a=1;}else if(e.getSource()==LPressButton2){LWantLevel=2;a=1;}else if(e.getSource()==LPressButton3){LWantLevel=3;a=1;else if(e.getSource()==LPressButton4) {LWantLevel=4;a=1;}else if(e.getSource()==LPressButton5) {LWantLevel=5;a=1;}else if(e.getSource()==LPressButton6) {LWantLevel=6;a=1;}else if(e.getSource()==LPressButton7) {LWantLevel=7;a=1;}else if(e.getSource()==LPressButton8) {LWantLevel=8;a=1;}else if(e.getSource()==LPressButton9) {LWantLevel=9;a=1;}else if(e.getSource()==LPressButton10) {LWantLevel=10;a=1;}else if(e.getSource()==RPressButton1) {RWantLevel=1;b=1;}else if(e.getSource()==RPressButton2) {RWantLevel=2;b=1;}else if(e.getSource()==RPressButton3) {RWantLevel=3;b=1;}else if(e.getSource()==RPressButton4) {RWantLevel=4;b=1;else if(e.getSource()==RPressButton5){RWantLevel=5;b=1;}else if(e.getSource()==RPressButton6){RWantLevel=6;b=1;}else if(e.getSource()==RPressButton7){RWantLevel=7;b=1;}else if(e.getSource()==RPressButton8){RWantLevel=8;b=1;}else if(e.getSource()==RPressButton9){RWantLevel=9;b=1;}else if(e.getSource()==RPressButton10){RWantLevel=10;b=1;}else{;}}}}/*//////////////////////////////////////////////////////////////////////////////////文件说明:程序用java语言编写,自行安装JDK(下载网址:)。