java同一包下的两个文件,一个文件中的函数调用另一个文件中的函数失败?

分成两个文件写
问题:
第二个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"));
			}
		}
	}
}

http://img3.mukewang.com/5a6d48390001be8b19201030.jpg

慕粉18341035298
浏览 3322回答 1
1回答

MR帽子先生

 private只能在本包内调用啊,你改成public就行了。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java