[英]ActionAdapter cannot be resolved to a type
我已经开始学习有关事件处理程序的知识,并尝试了一些基本概念。 我在以下代码中遇到了一个奇怪的错误。 我正在使用Eclipse,并且在尝试将ActionListener添加到Button b的行上出现以下错误:
“ Button类型的方法addActionListener(ActionListener)不适用于new ActionAdapter(){}类型。”
“ ActionAdapter无法解析为类型”
import java.awt.*;
import java.awt.event.*;
public class Test extends Frame{
private TextField text = new TextField(20);
Button b;
private int num_clicks = 0;
public static void main(String args[]){
Test appwind = new Test("title");
appwind.setSize(300,300);
appwind.setTitle("Irrelevant");
appwind.setVisible(true);
}
public Test(String title)
{
super(title);
setLayout(new FlowLayout());
addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e) {
System.exit(0);}});
b = new Button("Click");
add(b);
add(text);
b.addActionListener(
new ActionAdapter()
{
public void actionPerformed(ActionEvent e)
{
num_clicks++;
text.setText("number of times clicked: " + num_clicks);}
});
}
}
适配器类是当基础接口具有2个或更多方法时提供的便捷类。 ActionListener
只有一种方法-改用ActionListener
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
...
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.