为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕粉18341035298 2018-01-28 11:49:28
分成两个文件写 问题: 第二个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")); } } } }
查看完整描述

1 回答

?
MR帽子先生

TA贡献245条经验 获得超106个赞

 private只能在本包内调用啊,你改成public就行了。

查看完整回答
反对 回复 2018-02-01
  • 慕粉18341035298
    慕粉18341035298
    我把所有private改成了public,还是不行。我这两个文件应该是同一包内的吧
  • 1 回答
  • 0 关注
  • 3319 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信