我想创建一个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/