标题:[求助]关于监听器的问题
取消只看楼主
eaglesnail
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-6-5
 问题点数:0 回复次数:3 
[求助]关于监听器的问题

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());
}
}
}
那两个监听器谁放前面就响应谁.
谁能告诉我该怎么改吗?

搜索更多相关主题的帖子: 电梯 import awt 监听 java 
2007-06-08 12:46
eaglesnail
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-6-5
得分:0 
你打死我算了
2007-06-08 13:00
eaglesnail
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-6-5
得分:0 
苍天啊,大地啊
2007-06-08 13:02
eaglesnail
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-6-5
得分:0 
谢谢楼上的建议,请问你一般用什么?
2007-06-08 15:17



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-145892-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.098143 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved