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")); } } }} |
免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。
版权声明:作者保留权利,不代表天盟立场。
|
|
|
|
private只能在本包内调用啊,你改成public就行了。 |
|
|
|
|