ActionListener 是抽象的并且不会覆盖抽象方法 actionPerformed —— 尽管包含那个非常方法 [英] ActionListener is abstract and does not override abstract method actionPerformed -- despite containing that very method

查看:27
本文介绍了ActionListener 是抽象的并且不会覆盖抽象方法 actionPerformed —— 尽管包含那个非常方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我收到错误 Class is not abstract 并且不会覆盖 ActionListener 中的抽象方法 actionPerformed(ActionEvent)

我知道这个错误的根源,但让我困惑的是我有那个方法.

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.

包括导入和相关方法的上下文

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);

}'

推荐答案

问题不在您发布的原始代码段中.您的 Clock 类没有实现 actionPerformed().taskPerformer 对象可以.但是代码抱怨 taskPerformer 是一个 ActionListener 而 Clock 不是.

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.

此外,您似乎没有在任何地方使用 taskPerformer 那么为什么需要匿名类.您可以在 Clock 中定义 actionPerformed() 并完成它.

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.

我有两种方法可以解决这个问题.(为了使技术更清晰,我省略了一些不相关的代码).

I see two ways to deal with this. (I've omitted some irrelevant code to make the technique clearer).

选项 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);    
    }
}

选项 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);    
    }
}

这篇关于ActionListener 是抽象的并且不会覆盖抽象方法 actionPerformed —— 尽管包含那个非常方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆