java程序.哪错了?!!!

来源:百度知道 编辑:UC知道 时间:2024/05/25 04:12:57
import java.awt.*;
import java.awt.event.*;
public class TextEvent1 extends Frame
{
Label textString;
Label length;
TextField words;
public TextEvent1(String title)
{
super(title);
setLayout(new GridLayout(3,1));
textString = new Label("字符串");
length = new Label("长度");
words = new TextField(30);
add(textString);
add(length);
add(words);
}
class TextHander implements TextListener
{
public void textValueChange(TextEvent te)
{
textString.setText("字符串"+words.getText());
length.setText("字符数为:"+words.getText().length());
}
}
words.addTextListener(new TextHander());
public static void main(String args)
{
TextEvent1 te1 = new TextEvent1("Test");
te1.pack();
te1.show();
}
}
我不是要注册ActionListener.而是TextListener!

改完了,里面有4个地方有注意一下,我都注释了,看看吧.

import java.awt.*;
import java.awt.event.*;
public class TextEvent1 extends Frame
{
Label textString;
Label length;
TextField words;
public TextEvent1(String title)
{
super(title);
setLayout(new GridLayout(3,1));
textString = new Label("字符串");
length = new Label("长度");
words = new TextField(30);
add(textString);
add(length);
add(words);
//要注册监听的事件
words.addTextListener(new TextHander());
//1.还应该加上退出的事件,就象我运行的时候点退出都退出不了
//实现WindowAdapter里的windowClosing方法并注册到对象中
}
class TextHander implements TextListener
{
//2.原来的方法名少了个d,我晕
public void textValueChanged(TextEvent te)
{
textString.setText("字符串"+words.getText());
length.setText("字符数为:"+words.getText().length());
}

}
//3.这里是类的内部,怎么能有语句存在呢?我把语句放在了构造函数里.
//words.addTextListener(new TextHander());