java:按钮不是抽象的,不会覆盖 java.awt.event.ActionListener 中的抽象方法 actionPerformed(java.awt.event.ActionEvent)

问题描述

我是java新手,不知道如何解决这个问题。以下是代码显示错误

java: Button is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener

我已经在 mant 网站和 youtube 上进行了搜索,但仍然没有找到解决此问题的正确方法

public class Button implements ActionListener {
    JTextField jt1,jt2,jt3;
    JButton b1,b2,b3,b4,b5;

    Button() {
        JFrame jf = new JFrame();
        JLabel jl1 = new JLabel("1st no:");
        jl1.setBounds(40,40,30,30);

        JLabel jl2 = new JLabel("2nd no:");
        jl2.setBounds(40,90,30);

        JLabel jl3 = new JLabel("Result:");
        jl3.setBounds(40,150,30);

        jt1 = new JTextField();
        jt1.setBounds(90,100,30);

        jt2 = new JTextField();
        jt2.setBounds(90,30);

        jt3 = new JTextField();
        jt3.setBounds(90,30);
        jt3.setEditable(false);

        b1 = new JButton("+");
        b1.setBounds(40,200,30);
        b2 = new JButton("-");
        b2.setBounds(80,30);
        b3 = new JButton("*");
        b3.setBounds(120,30);
        b4 = new JButton("/");
        b4.setBounds(160,30);
        b5 = new JButton("%");
        b5.setBounds(200,30);
        jf.setLayout(null);
        jf.add(jl1);
        jf.add(jl2);
        jf.add(jt1);
        jf.add(jl3);
        jf.add(jt2);
        jf.add(jt3);
        jf.add(b1);
        jf.add(b2);
        jf.add(b3);
        jf.add(b5);
        jf.add(b4);
        jf.setSize(300,400);
        jf.setVisible(true);

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);

    }

    public void ActionListener(ActionEvent e) {
        String a = jt1.getText();
        String b = jt2.getText();
        int n = Integer.parseInt(a);
        int m = Integer.parseInt(b);
        if (e.getSource() == b1) {
            int x = n + m;
            String r = String.valueOf(x);
            jt3.setText(r);
        } else if (e.getSource() == b2) {
            int x = n - m;
            String r = String.valueOf(x);
            jt3.setText(r);

        } else if (e.getSource() == b3) {
            int x = n / m;
            String r = String.valueOf(x);
            jt3.setText(r);

        } else if (e.getSource() == b4) {
            int x = n * m;
            String r = String.valueOf(x);
            jt3.setText(r);

        } else if (e.getSource() == b5) {
            int x = n % m;
            String r = String.valueOf(x);
            jt3.setText(r);

        }
    }

    public static void main(String[] args) {
        new Button();
    }
}

解决方法

当您为任何类实现接口时,您需要覆盖类中所有非抽象的接口方法。

当你的 Button 类实现 ActionListener 接口时,你需要在你的类中覆盖它的方法。

,

代码有很多需要改进的地方,但是关于错误的问题 Button is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener, 这意味着您应该实现 actionPerformed 方法。所以把public void ActionListener(ActionEvent e)改成public void actionPerformed(ActionEvent e),错误就会消失:

public void actionPerformed(ActionEvent e) {
    String a = jt1.getText();
    String b = jt2.getText();
    ...