user3078082
user3078082

Reputation: 5

method addActionListener in class abstractButton cannot be applied to the given type

I'm trying to make a Calculator using swing but I stuck with a problem when I compiled it, It is throughing an error that method addActionListener in class AbstractButton cannot be applied to given type.

my code is

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

Upvotes: 1

Views: 1841

Answers (1)

Radiodef
Radiodef

Reputation: 37845

button[i].addActionListener(this);

This appears inside Centerwindow which does not implement ActionListener. Because Centerwindow is an inner class, you can access the enclosing Calculator instance and add it if that's what you're trying to do:

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

Upvotes: 5

Related Questions