分成两个文件写
问题:
第二个listenns文件中函数没有被调用,原因是生成的界面上start按钮和右上角的关闭按钮点击
无反应,请问原因是什么
test文件
package SecondJava;
import java.awt.*;
import java.awt.event.*;
public class test extends Frame implements ActionListener{
private Button start=new Button("Start");
private Button show=new Button("Show");
private Button close=new Button("close");
private TextField display=new TextField(25);
private listens count=new listens(show,25);
private class WindowCloser extends WindowAdapter{
public void WindowClosing(WindowEvent we){
System.exit(0);
}
}
public test(){
super("CountDown Tester");
setLayout(new FlowLayout());
addWindowListener(new WindowCloser());
add(start);
add(show);
add(display);
add(close);
close.addActionListener(this);
show.addActionListener(this);
start.addActionListener(this);
count.addActionListener(this);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==start){
count.startCounting();
}
else if(e.getSource()==show)display.setText("Event came from:"+e.getActionCommand());
else if(e.getSource()==close)System.exit(0);
}
public static void main(String args[]){
test c=new test();
}
}
listens文件
package SecondJava;
import java.awt.*;
import java.awt.event.*;
public class listens {
ActionListener listener=null;
Object source=null;
int maxCount=10;
public listens(Object _source,int _maxCount){
maxCount=_maxCount;
source=_source;
}
public void addActionListener(ActionListener newListener){
listener=AWTEventMulticaster.add(listener, newListener);
}
public void removeActionListener(ActionListener oldListener){
listener=AWTEventMulticaster.remove(listener, oldListener);
}
public void startCounting(){
if(listener==null){
for(int i=maxCount;i>=0;i--){
System.out.println("i:"+i);
System.out.println("Done.Generating event now...");
listener.actionPerformed(new ActionEvent(source,ActionEvent.ACTION_PERFORMED,"CountDown"));
}
}
}
}
添加回答
举报
0/150
提交
取消