Timer / ActionListener用法

时间:2013-05-07 04:10:53

标签: java timer compiler-errors awt actionlistener

我想这很简单,但我不能说出错是什么。

    final Timer tiempo= new Timer(1000,new ActionListener()) ;

我唯一想要的是,延迟1000和动作监听器,我完全不明白。问题是我总是犯错误。 “未定义。”

使用方法

        final Timer tiempo= new Timer(1000, ActionListener() 
    { 
        public void actionPerformed(ActionEvent e) 
        {   

        }
    };    

仍然未定义并尝试在

之前制作实例
ActionListener actionlistener= new ActionListener();
            final Timer tiempo= new Timer(1000, actionlistener() 
    { 
        public void actionPerformed(ActionEvent e) 
        {   

        }
    };    

请解释我,这非常令人沮丧。

Error: ActionListner cannot be resolved to a variable

1 个答案:

答案 0 :(得分:4)

在您的第一个示例中,您尚未“创建”新的ActionListener

final Timer tiempo= new Timer(1000, ActionListener() 
{ 
    public void actionPerformed(ActionEvent e) 
    {   

    }
};  

应为new ActionListener()

final Timer tiempo= new Timer(1000, new ActionListener() 
{ 
    public void actionPerformed(ActionEvent e) 
    {   

    }
};  

你的第二个例子出于多种原因是错误的......

ActionListener actionlistener= new ActionListener(); // This won't compile
// because it is an interface and it requires either a concrete implementation
// or it's method signatures filled out...

// This won't work, because java sees actionlistener() as method, which does not
// exist and the rest of the code does not make sense after it (to the compiler)
final Timer tiempo= new Timer(1000, actionlistener() 
{ 
    public void actionPerformed(ActionEvent e) 
    {   

    }
}; 

看起来应该更像......

ActionListener actionlistener= new ActionListener()
{ 
    public void actionPerformed(ActionEvent e) 
    {   

    }
}; 
final Timer tiempo= new Timer(1000, actionlistener);