⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dianti.java

📁 基于java语言实现的模拟电梯运行的仿真程序
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*; 
import javax.swing.*;
 
public class DianTi extends JFrame{ 
   public static void main(String []args){ 
       SetFrame sf=new SetFrame();
       sf.setSize(400,300);
       sf.setTitle("电梯"); 
       sf.setVisible(true); 
       sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       Action act=new Action(sf); 
   } 
}

class SetButtonPanel extends JPanel{
   JButton jbt[]=new JButton[20]; 
   public SetButtonPanel(){ 
       setLayout(new GridLayout(11,2));
       for(int i=0;i<20;i++){ 
           jbt=new JButton(""+(i+1));
           add(jbt); 
       } 
   } 
} 
 
class SetShow extends JPanel{
   JLabel jlb[]=new JLabel[20]; 
   public SetShow(){
       setLayout(new GridLayout(2,10)); 
       for(int i=0;i<20;i++){ 
           jlb=new JLabel(""+(i+1)+"层"); 
           jlb.setFont(new Font("",Font.BOLD,12)); 
           add(jlb); 
       }
   }
}
  
class StringPanel extends JPanel{
   JLabel state=new JLabel("门关上了");
   JLabel num=new JLabel("目前人数");
   public StringPanel(){ 
       setLayout(new FlowLayout(FlowLayout.CENTER)); 
       state.setFont(new Font("宋体",Font.BOLD,20));
       state.setForeground(Color.RED); 
       num.setFont(new Font("宋体",Font.BOLD,20));
       num.setForeground(Color.BLUE);
       add(state); 
       add(num); 
   } 
   public void setState(boolean stt){
       if(stt==true){
           state.setText("门关上了");
       }else{
           state.setText("门开了");
       }  
   }  
   public void SetNum(int n){
       num.setText("目前人数: "+n+"人"); 
   } 
} 

class SetFrame extends JFrame{ 
   Container con=getContentPane(); 
   SetButtonPanel sbp=new SetButtonPanel(); 
   SetShow ss=new SetShow();
   StringPanel sp=new StringPanel();
   public SetFrame(){
       con.setLayout(new BorderLayout(5,5));
       con.add(ss,BorderLayout.NORTH);
       con.add(sbp,BorderLayout.EAST); 
       con.add(sp,BorderLayout.CENTER);
   }
}  

class Action extends JFrame implements Runnable{
   SetFrame sf; 
   Thread runner;
   int floor=0;
   int floor_end=1;
   int person_num=0;
   public Action(SetFrame stfm){
       sf=stfm;
       sf.ss.jlb[0].setForeground(Color.GREEN);
       for(int i=0;i<20;i++){ 
           sf.sbp.jbt.addActionListener(new MyActionListener());
           sf.sbp.jbt.setActionCommand(""+(i+1));
       }
   }
   class MyActionListener implements ActionListener{
       public void actionPerformed(ActionEvent ae){
           floor=Integer.parseInt(ae.getActionCommand());
           sf.sbp.jbt[floor-1].setForeground(Color.BLUE);
           person_num=(int)(1+Math.random()*20); 
           threadStart();
       } 
   } 
   public void threadStart(){
       runner=new Thread(this);
       runner.start();
   }
   public void run(){
       while(true){
           if(floor_end<floor){ 
               up();
           }else if(floor_end>floor){
               down(); 
           }else if(floor_end==floor){
               stop();
           }
           
       } 
   }
   public void up(){
       sf.sp.setState(true);
       sf.sp.SetNum(person_num);  
       pause(1000); 
       sf.ss.jlb[floor_end].setForeground(Color.GREEN);  
       sf.ss.jlb[floor_end-1].setForeground(Color.BLACK);  
       floor_end++;
   } 
   public void down(){
       sf.sp.setState(true);
       sf.sp.SetNum(person_num); 
       pause(1000);
       sf.ss.jlb[floor_end-2].setForeground(Color.GREEN); 
       sf.ss.jlb[floor_end-1].setForeground(Color.BLACK);
       floor_end--;
   }  
   public void stop(){
       if(floor==1){
           pause(1000);
           floor_end=0; 
           sf.ss.jlb[0].setForeground(Color.GREEN);
           sf.ss.jlb[1].setForeground(Color.BLACK); 
           floor_end=1;
       } 
       sf.sp.setState(false);
       sf.sbp.jbt[floor-1].setForeground(Color.BLACK);
       runner.stop(); 
   }
   public void pause(int time){
       try{ 
           runner.sleep(time); 
       }catch(InterruptedException ire){
           System.err.println(ire.getMessage()); 
       }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -