1

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

    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
4

1 回答 1

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);
于 2013-05-07T04:16:29.227 回答