"The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments" error

Arnav Kumar :

I wanted to create a JFrame where it prints out on the console: "It works!!" when you click a JButton. Below is the code:

import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;

public class CurrentlyMajorCodesCompiler extends JFrame {

public static void main (String args[]) {
CurrentlyMajorCodes CMC = new CurrentlyMajorCodes();

CMC.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

public class CurrentlyMajorCodes extends JFrame {

private JButton ClickSpeedTest;
private tensCPS TCPS;

public CurrentlyMajorCodes () {
    super("Major Code Compiler");
    setLayout(new FlowLayout());

    ClickSpeedTest = new JButton("Click Speed Test");
    add(ClickSpeedTest);

    ClickSpeedTest.addActionListener(new MouseAdapter () {
        public void mouseClicked (MouseEvent event) {
            System.out.println("It works!!");
        }
    });

    setSize(250, 250);
    setVisible(true);
}
}

However, at: ClickSpeedTest.addActionListener, it gives me an error saying:

The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (new MouseAdapter(){})

I don't understand what it's trying to communicate, because I never used an AbstractButton in the code, and don't know what it even is. Can someone please help?

ControlAltDel :

MouseListener is different from ActionListener. You need to use the later

ClickSpeedTest.addActionListener(new ActionListener () {
    public void actionPerformed (ActionEvent event) {
        System.out.println("It works!!");
    }
});

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=301095&siteId=1