Java ActionListener 是抽象的并且不会覆盖抽象方法 actionPerformed -- 尽管包含那个方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18813580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ActionListener is abstract and does not override abstract method actionPerformed -- despite containing that very method
提问by Cortney Reagle
So I'm getting the error Class is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
所以我收到错误 Class is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
I am aware of the origin of this error but what confuses me is that I have that method.
我知道这个错误的起源,但让我困惑的是我有那个方法。
public static ActionListener taskPerformer = new ActionListener(){
public void actionPerformed(ActionEvent e){
Clock cl = new Clock();
if(seconds<59){
seconds++;
}else{
seconds=0;
if(minutes<59){
minutes++;
}else{
minutes=0;
if(hours<12){
hours++;
}else{
hours=0;
}
}
}
cl.repaint();
}
};'
Not quite sure what's going on. Any help? Thanks in advance.
不太确定发生了什么。有什么帮助吗?提前致谢。
EDIT: context including imports and related methods
编辑:上下文包括导入和相关方法
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Object.*;
import java.text.DecimalFormat;
import java.awt.geom.*;
public class Clock extends JFrame implements ActionListener{
public static int seconds = 0;
public static int minutes = 0;
public static int hours = 9;
public static ActionListener taskPerformer = new ActionListener(){
public void actionPerformed(ActionEvent e){
Clock cl = new Clock();
if(seconds<59){
seconds++;
}else{
seconds=0;
if(minutes<59){
minutes++;
}else{
minutes=0;
if(hours<12){
hours++;
}else{
hours=0;
}
}
}
cl.repaint();
}
};
public static Timer timer = new Timer(1000, taskPerformer);
public static void main(String[] args){
Clock cl = new Clock();
init();
SwingUtilities.invokeLater(new Runnable(){
public void run() {
createAndShowGUI();
}
});
}
public static void init(){
timer.start();
}
public Clock() {
super("Clock");
timer.addActionListener(this);
}'
采纳答案by Jeanne Boyarsky
The problem wasn't in the original code snippet you posted. Your class Clock does not implement actionPerformed(). The taskPerformer object does. But the code is complaining that taskPerformer is an ActionListener but Clock is not.
问题不在您发布的原始代码段中。你的类 Clock 没有实现 actionPerformed()。taskPerformer 对象可以。但是代码抱怨 taskPerformer 是一个 ActionListener 而 Clock 不是。
Also, you don't appear to use taskPerformer anywhere so why do you need the anonymous class. You could just define actionPerformed() in Clock and be done with it.
此外,您似乎没有在任何地方使用 taskPerformer 那么为什么需要匿名类。您可以在 Clock 中定义 actionPerformed() 并完成它。
I see two ways to deal with this. (I've omitted some irrelevant code to make the technique clearer).
我看到有两种方法可以解决这个问题。(为了使技术更清晰,我省略了一些不相关的代码)。
Option 1 - Use clock as the listener
选项 1 - 使用时钟作为侦听器
public class Clock extends JFrame implements ActionListener {
public static int seconds = 0;
public static int minutes = 0;
public static int hours = 9;
public void actionPerformed(ActionEvent e) {
// code
}
public Timer timer;
public Clock() {
super("Clock");
timer = new Timer(1000, this);
timer.addActionListener(this);
}
}
Option 2 - Use the anonymous listener
选项 2 - 使用匿名侦听器
public class Clock extends JFrame {
public static int seconds = 0;
public static int minutes = 0;
public static int hours = 9;
public static ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// insert code here
}
};
public static Timer timer = new Timer(1000, taskPerformer);
public Clock() {
super("Clock");
timer.addActionListener(taskPerformer);
}
}
回答by Bribe Guntails
A simple way of putting is that, if a class implements ActionPerformed, the action must be performed within that classes boundaries.
一种简单的放置方法是,如果一个类实现了 ActionPerformed,则该操作必须在该类的边界内执行。
回答by Nitin Guda
Try adding java.awt.event to your code as follows :
尝试将 java.awt.event 添加到您的代码中,如下所示:
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
//do something here
}
Had the same problem. Worked for me.
有同样的问题。为我工作。