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(400,300);
sf.setTitle("电梯");
sf.setVisible(true);
sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Action act=new Action(sf);
}
}
//电梯外部按扭的面板
class SetOutterButton extends JPanel{
JButton outbt[]=new JButton[10];
SetButtonPanel SetButton;
public SetOutterButton(){
setLayout(new GridLayout(6,2));
for(int i=0;i<10;i++){
outbt[i]=new JButton(""+(i+1));
add(outbt[i]);
}
}
}
//电梯内部按扭的面板
class SetInnerButton extends JPanel{
JButton inbt[]=new JButton[10];
SetButtonPanel SetButton;
public SetInnerButton(){
setLayout(new GridLayout(6,2));
for(int i=0;i<10;i++){
inbt[i]=new JButton(""+(i+1));
add(inbt[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 num=new JLabel("目前人数");
public StringPanel(){
num.setFont(new Font("宋体",Font.BOLD,20));
num.setForeground(Color.BLUE);
add(num);
}
}
class SetFrame extends JFrame{
Container con=getContentPane();
SetOutterButton outb=new SetOutterButton();
SetInnerButton inb = new SetInnerButton();
SetShow ss=new SetShow();
StringPanel sp=new StringPanel();
public SetFrame(){
con.setLayout(new BorderLayout(5,5));
con.add(ss,BorderLayout.NORTH);
con.add(inb,BorderLayout.WEST);
con.add(outb,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.outb.outbt[i].addActionListener(new MyOutActionListener());
sf.outb.outbt[i].setActionCommand(""+(i+1));
}
for(int i=0;i<20;i++){
sf.inb.inbt[i].addActionListener(new MyInActionListener());
sf.inb.inbt[i].setActionCommand(""+(i+1));
}
}
class MyOutActionListener implements ActionListener{
public void actionPerformed(ActionEvent outae){
floor=Integer.parseInt(outae.getActionCommand());
sf.outb.outbt[floor-1].setForeground(Color.BLUE);
sf.sp.num.setText("门关了,进去"+(int)(1+Math.random()*20)+"人");
threadStart();
}
}
class MyInActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
floor=Integer.parseInt(ae.getActionCommand());
sf.inb.inbt[floor-1].setForeground(Color.BLUE);
sf.sp.num.setText("门开了,出去"+(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(){
pause(1000);
sf.ss.jlb[floor_end].setForeground(Color.GREEN);
sf.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end++;
}
public void down(){
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.outb.outbt[floor-1].setForeground(Color.BLACK);
runner.stop();
}
public void pause(int time){
try{
runner.sleep(time);
}catch(InterruptedException ire){
System.err.println(ire.getMessage());
}
}
}
那两个监听器谁放前面就响应谁.
谁能告诉我该怎么改吗?