Java 类abstractButton中的方法addActionListener无法应用于给定类型

Java 类abstractButton中的方法addActionListener无法应用于给定类型,java,swing,Java,Swing,我试图使用swing制作一个计算器,但在编译它时遇到了一个问题,即类AbstractButton中的方法addActionListener无法应用于给定类型 我的代码是 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends JFrame implements ActionListener { private String str = "0";

我试图使用swing制作一个计算器,但在编译它时遇到了一个问题,即类AbstractButton中的方法addActionListener无法应用于给定类型

我的代码是

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame implements ActionListener {
    private String str = "0";
    JTextField l_tf = new JTextField(100);
    JButton button[] = new JButton[31];
    Northwindow panelNorth = new Northwindow();
    Centerwindow panelCenter = new Centerwindow();

    Calculator(String l_s) {
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add("North", panelNorth);
        getContentPane().add("Center", panelCenter);

        setSize(300, 400);
        setVisible(true);
    }

    //Northwindow

    class Northwindow extends JPanel {
        public Northwindow() {
            Dimension textf = new Dimension(50, 50);
            setLayout(new GridLayout(0, 1));
            setBackground(new Color(230, 230, 255));
            l_tf.setPreferredSize(textf);
            add(l_tf);
        }
    }


    class Centerwindow extends JPanel {
        public Centerwindow() {
            setLayout(new GridLayout(0, 4));
            String key[] = {"ON", "Del", "C", "+/-", "1/x", "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+", "Sqrt", "Sqr", "Sin", "Cos", "^", "log", "ln", "tan", "(", ")", "pie"};

            Dimension but = new Dimension(80, 30);
            for (int i = 0; i < button.length; i++) {

                button[i] = new JButton(key[i]);
                button[i].setPreferredSize(but);
                add(button[i]);
                button[i].addActionListener(this);

                //before the on button is not clicked

                for (i = 0; i < button.length; i++)
                    button[i].setEnable(false);
                l_tf.setEditable(false);

            }
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource == button[0])  // ON button
        {
            l_tf.setBackground(color.white);
            button[0].setLabel("OFF");
            for (i = 0; i < button.length; i++)
                button[i].setEnable(true);
            l_tf.setEditable(true);
            l_tf.setText(str);
        }
        if (e.getSource == button[1]) {
        }
    }

    public static void main(String... s) {
        Calculator c = new Calculator("Calculator");
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类计算器扩展JFrame实现ActionListener{
私有字符串str=“0”;
JTextField l_tf=新的JTextField(100);
JButton按钮[]=新JButton[31];
Northwindow panelNorth=新的Northwindow();
中心窗面板中心=新的中心窗();
计算器(字符串l_s){
getContentPane().setLayout(新的BorderLayout());
getContentPane().add(“北”,panelNorth);
getContentPane()。添加(“中心”,panelCenter);
设置大小(300400);
setVisible(真);
}
//北窗
类Northwindow扩展了JPanel{
公共窗口(){
尺寸文本f=新尺寸(50,50);
setLayout(新的GridLayout(0,1));
挫折背景(新颜色(230、230、255));
l_tf.setPreferredSize(textf);
添加(l_tf);
}
}
类Centerwindow扩展了JPanel{
公共中心窗口(){
setLayout(新的GridLayout(0,4));
字符串键[]={“ON”、“Del”、“C”、“+/-”、“1/x”、“7”、“8”、“9”、“6”、“4”、“5”、“6”、“6”、“1”、“2”、“3”、“-”、“0”、“0”、“0”、“Sqrt”、“Sqr”、“Sin”、“Cos”、“^”、“log”、“ln”、“tan”、“tan”、“pie”};
尺寸但=新尺寸(80,30);
对于(int i=0;i
这将出现在未实现
ActionListener
Centerwindow中。由于
Centerwindow
是一个内部类,因此您可以访问封闭的
Calculator
实例并添加它(如果您正试图这样做):

button[i].addActionListener(Calculator.this);
这将出现在未实现
ActionListener
Centerwindow中。由于
Centerwindow
是一个内部类,因此您可以访问封闭的
Calculator
实例并添加它(如果您正试图这样做):

button[i].addActionListener(Calculator.this);