java - "The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments"错误

标签 java swing awt jbutton mouselistener

我想创建一个JFrame它在控制台上打印出来:“它有效!!”当您单击JButton时。下面是代码:

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

但是,位于:ClickSpeedTest.addActionListener ,它给了我一个错误:

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

我不明白它想传达什么,因为我从未使用过 AbstractButton在代码中,甚至不知道它是什么。有人可以帮忙吗?

最佳答案

MouseListener 与 ActionListener 不同。您需要使用稍后的内容

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

关于java - "The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60727421/

相关文章:

java - 使用来自 java 的 OLE 自动化将 word 文件拆分为多个较小的 word 文件

java - 使用 JPA Criteria Query 比较两个日期之间的差异

java - 在 Windows 10 上,控制台窗口不接受键盘输入

java - 动态创建JTextAreas?

java - 为什么这个JAVA代码无法解析构造函数?

java - 在Spring Batch中,如何以给定的项目列表作为参数,在批量读取项目列表后插入一段代码?

java - 绑定(bind)两个 JComboBoxes

java - isEventDispatchThread 超出 EDT 但从 EventDispatchThread.run 运行

Java KeyListener 与移动对象

java - Rectangle.getX() 返回上角还是中心?