谢谢你啊
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class lift extends JFrame{
public static void main(String []args){
SetFrame sf=new SetFrame();
sf.setSize(1000,300);
sf.setTitle("电梯");
sf.setVisible(true);
sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionTwo actTwo=new ActionTwo(sf);
ActionOne actOne=new ActionOne(sf);
}
}
class ActionOne extends JFrame implements Runnable{
SetFrame sf;
Thread runnerOne;
int floor=0;
int floor_end=1;
int person_num=0;
public ActionOne(SetFrame stfm){
sf=stfm;
sf.liftOne.ss.jlb[0].setForeground(Color.GREEN);//开始第一层为绿色
for(int i=0;i<20;i++){
sf.liftOne.sbp.jbt[i].addActionListener(new MyActionListener());
sf.liftOne.sbp.jbt[i].setActionCommand(""+(i+1));
}
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
floor=Integer.parseInt(ae.getActionCommand());
sf.liftOne.sbp.jbt[floor-1].setForeground(Color.BLUE);
person_num=(int)(1+Math.random()*20);//电梯最多20人
threadStart();
}
}
public void threadStart(){
runnerOne=new Thread(this);
runnerOne.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.liftOne.sp.setState(true);
sf.liftOne.sp.SetNum(person_num);
pause(1000);
sf.liftOne.ss.jlb[floor_end].setForeground(Color.GREEN);
sf.liftOne.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end++;
}
public void down(){
sf.liftOne.sp.setState(true);
sf.liftOne.sp.SetNum(person_num);
pause(1000);
sf.liftOne.ss.jlb[floor_end-2].setForeground(Color.GREEN);
sf.liftOne.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end--;
}
public void stop(){
if(floor==1){
pause(1000);
floor_end=0;
sf.liftOne.ss.jlb[0].setForeground(Color.GREEN);
sf.liftOne.ss.jlb[1].setForeground(Color.BLACK);
floor_end=1;
}
sf.liftOne.sp.setState(false);
sf.liftOne.sbp.jbt[floor-1].setForeground(Color.BLACK);
runnerOne.stop();
}
public void pause(int time){
try{
runnerOne.sleep(time);
}catch(InterruptedException ire){
System.err.println(ire.getMessage());
}
}
}
class ActionTwo extends JFrame implements Runnable{
SetFrame sf;
Thread runner;
int floor=0;
int floor_end=1;
int person_num=0;
public ActionTwo(SetFrame stfm){
sf=stfm;
sf.liftTwo.ss.jlb[0].setForeground(Color.GREEN);//开始第一层为绿色
for(int i=0;i<20;i++){
sf.liftTwo.sbp.jbt[i].addActionListener(new MyActionListener());
sf.liftTwo.sbp.jbt[i].setActionCommand(""+(i+1));
}
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
floor=Integer.parseInt(ae.getActionCommand());
sf.liftTwo.sbp.jbt[floor-1].setForeground(Color.BLUE);
person_num=(int)(1+Math.random()*20);//电梯最多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.liftTwo.sp.setState(true);
sf.liftTwo.sp.SetNum(person_num);
pause(1000);
sf.liftTwo.ss.jlb[floor_end].setForeground(Color.GREEN);
sf.liftTwo.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end++;
}
public void down(){
sf.liftTwo.sp.setState(true);
sf.liftTwo.sp.SetNum(person_num);
pause(1000);
sf.liftTwo.ss.jlb[floor_end-2].setForeground(Color.GREEN);
sf.liftTwo.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end--;
}
public void stop(){
if(floor==1){
pause(1000);
floor_end=0;
sf.liftTwo.ss.jlb[0].setForeground(Color.GREEN);
sf.liftTwo.ss.jlb[1].setForeground(Color.BLACK);
floor_end=1;
}
sf.liftTwo.sp.setState(false);
sf.liftTwo.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());
}
}
}