JAVA小程序帮忙看下错在哪里

来源:百度知道 编辑:UC知道 时间:2024/05/26 06:53:40
package ConsoleApplication1;

import java.awt.*;
import java.awt.event.*;

public class TextFieldExample implements ActionListener, TextListener
{
Frame f;
Label lb;
TextField tf;
public static void main(String[] args)
{
new TextFieldExample();
}

public TextFieldExample()
{
f = new Frame("TextField Example");
f.setLayout(new GridLayout(2, 1));
tf = new TextField("", 20);
tf.addTextListener(this);
tf.addActionListener(this);
lb = new Label();
f.add(tf);
f.add(lb);
f.pack();
f.setVisible(true);
}
public void textValueChanged(TextEvent e)
{
tf.setText(tf.getText());
}
public void actionPerformed(TextEvent e)
{
tf.setText("");
}
}

提示错误:'ConsoleApplication1.TestFieldExample'must be declared 'abstract'or 'java.awt

问题说的很明白,如果要implement一个interface,不是说仅仅在脑袋上加一个implement ActionListener的声明就可以,而是要实现这个ActionListener里面的所有声明的函数。而你的就是没有实现ActionListener里面的
public void actionPerformed(ActionEvent e)这个函数。你在你的类里面加上上面这个函数就好了。而你在类里面实现的public void actionPerformed(TextEvent e) 这个函数是另一个接口TextListener的,这个参数不同,不能混淆。

你实现的是actionPerformed(TextEvent)方法而不是actionPerformed(ActionEvent e)方法。