文本事件
文本事件即代表文本区域中文本变化的事件
TEXT_VALUE_CHANGED,在文本区域中改变文本内容。
public void addTextListener(TextLister 1)
添加特定的文本事件,监听者接收来自文本对象的文本事件。如果1为空,那么不会抛出任何异常,而且也不会完成任何动作。
public interface TextListener extends EventLister
用于接收文本事件的监听者接口。当对象的文本发生变化时,调用监听者对象的方法。
接口中的方法为:
public void textValueChanged(TextEvent e)
当文本发生改变时调用。
public object getSoure()
发生事件的对象,从EventObject继承来的方法。
程序例子: 测试文本事件。
//程序文件名为Test.java
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
public class Test extends Applet implements ActionListener,TextListener{
TextField told;
TextArea tnew;
Panel p;
public void init() {
told=new TextField(25);
tnew=new TextArea(8,25);
//添加事件监听者
told.addActionListener(this);
told.addTextListener(this);
//设置界面
p=new Panel(new BorderLayout());
p.add(told,BorderLayout.NORTH);
p.add(tnew,BorderLayout.SOUTH);
add(p);
}
//响应文本事件
public void textValueChanged(TextEvent e) {
if(e.getSource()==told)
tnew.setText(told.getText());
}
//响应动作事件
public void actionPerformed(ActionEvent e) {
if(e.getSource()==told)
tnew.setText("");
}
}