[CODE]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<10;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<10;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){
System.out.println("点了");
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());
}
}
}
class SetButtonPanel extends JPanel{
JButton jbt[]=new JButton[10];
SetButtonPanel SetButton;
public SetButtonPanel(){
setLayout(new GridLayout(6,2));
for(int i=0;i<10;i++){
jbt[i]=new JButton(""+(i+1));
add(jbt[i]);
}
}
}
class SetShow extends JPanel{
JLabel jlb[]=new JLabel[10];
public SetShow(){
setLayout(new GridLayout(2,5));
for(int i=0;i<10;i++){
jlb[i]=new JLabel(""+(i+1)+"层");
jlb[i].setFont(new Font("",Font.BOLD,12));
add(jlb[i]);
}
}
}
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 LiftOne extends JPanel{
SetButtonPanel sbp=new SetButtonPanel();
SetShow ss=new SetShow();
StringPanel sp=new StringPanel();
public LiftOne(){
setLayout(new BorderLayout(5,5));
add(ss,BorderLayout.NORTH);
add(sbp,BorderLayout.EAST);
add(sp,BorderLayout.CENTER);
}
}
class LiftTwo extends JPanel{
SetButtonPanel sbp=new SetButtonPanel();
SetShow ss=new SetShow();
StringPanel sp=new StringPanel();
public LiftTwo(){
setLayout(new BorderLayout(5,5));
add(ss,BorderLayout.NORTH);
add(sbp,BorderLayout.EAST);
add(sp,BorderLayout.CENTER);
}
}
class SetFrame extends JFrame{
Container con=getContentPane();
LiftOne liftOne = new LiftOne();
LiftTwo liftTwo = new LiftTwo();
public SetFrame(){
con.setLayout(new BorderLayout(5,5));
con.add(liftOne,BorderLayout.EAST);
con.add(liftTwo,BorderLayout.WEST);
}
}[/CODE]
我的修改只是让你的程序可以跑,里面的一些东西自己去改
你的程序错误原因如下:
在初始化第一个的时候,抛出了数组下标越界异常
你的JButton只有10个,可是你却想给20个添加事件监听器,当然 就出错了,一旦出错,下面的语句将不会执行.现在我把它改成了10的循环.
这些细节一定要小心.还有,应该要有排错的能力.
可惜不是你,陪我到最后