把TimePrinter类声明为:
static class TimePrinter implements ActionListener{
   ....
}
或者干脆把TimePrinter变为一个独立的类:
class TimePrinter implements ActionListener{
   ....
}
public class TimerTest{
....
}
两种方法可解决问题

解决方案 »

  1.   

    to heifei:
       按你说的改了后是成功了,希望能再回答我一个问题然后给分给你。
       这里的静态方法应该是main()了,而把listener当作静态变量了,但是为什么listener,但是我怎么知道它是静态的还是不是静态的?
      

  2.   

    这里存在一个创建的顺序问题,在同一个类内部,静态块是最先被创建的!这个时候TimePrinter还不存在!
      

  3.   

    原来程序改为:为什么要这样:因为main的问题。
    main只是java程序的入口点,不属于任何一个类的方法。import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.Timer;
    import javax.swing.event.*;public class TimerTest
    {  public static void main(String[] args)
        {        TimerTest t=new TimerTest();        JOptionPane.showMessageDialog(null,"quit program");
            System.exit(0);
        }    public TimerTest()
        {
          ActionListener listener=new TimePrinter();
          Timer t=new Timer(10000,listener);
          t.start();
        }    class TimePrinter implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                Date now=new Date();
                System.out.println("at the tone,the time is"+now);
                Toolkit.getDefaultToolkit().beep();
            }
        }
    }
      

  4.   

    upc_chenli(chenli) 回答得很清楚