ITeye博客

java TextListener的使用

bcworld · 2012-05-26 19:57 · Java SE · 编程语言 · Markdown

package gui.event;

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.JPanel;

public class UseTextEvent extends Applet implements ActionListener,
		TextListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private TextField tOld;
	private TextArea tNew;
	private JPanel p;

	public void init() {
		tOld = new TextField(25);
		tNew = new TextArea(8, 25);
		// 添加事件监听者
		tOld.addActionListener(this);
		tOld.addTextListener(this);
		// 设置界面
		p = new JPanel(new BorderLayout());
		p.add(tOld, BorderLayout.NORTH);
		p.add(tNew, BorderLayout.SOUTH);
		this.setSize(300, 240);
		this.add(p);
	}

	// 响应文本事件
	public void textValueChanged(TextEvent e) {
		if (e.getSource() == tOld)
			tNew.setText(tOld.getText());
	}

	// 响应动作事件
	public void actionPerformed(ActionEvent e) {
		// 按下Enter键时清空field
		if (e.getSource() == tOld)
			tNew.setText("");
	}
};

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。