我收到错误," AbstractButton类型中的方法addActionListener(ActionListener)不适用于参数(new ActionListener(){})"对于此代码:
package JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(800, 600);
frame.setVisible(true);
frame.setTitle("Title");
frame.setLayout(null);
frame.setResizable(false);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = frame.getSize().width;
int h = frame.getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
frame.setLocation(x, y);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b1 = new JButton("Press");
b1.setBounds(100, 100, 100, 100);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
}
}
答案 0 :(得分:2)
您的类名称也称为ActionListener,因此类型JFrame.ActionListener不适用于java.awt.event.ActionListener。您必须像这里一样限定班级的名称:
b1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
最重要的是重命名你的班级,以防止这种歧义。