import java.awt.*;
import java.awt.event.*;
public class TextExample implements ActionListener,TextListener{
Frame f;
TextField tf1;
TextArea ta1,ta2;
static public void main(String args[]){
TextExample te=new TextExample();
te.go();
}
public void go(){
f=new Frame("Text Example");
f.setLayout(new FlowLayout());
tf1=new TextField("Init",20);
tf1.addActionListener(this);
tf1.addTextListener(this);
ta1=new TextArea("Initial text",4,20);
ta1.addTextListener(this);
ta2=new TextArea("Only horizontal
scrollbar",4,30,TextArea.SCROLLBARS_HORIZONTAL_ONLY);
f.add(tf1);
f.add(ta1);
f.add(ta2);
f.setSize(300,300);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
ta2.append("/nActionPerformed");
}
public void textValueChanged(TextEvent e){
ta2.append("/nTextValueChanged");
}
}
本示例展示了如何使用 Java 的 AWT 库创建一个包含文本字段和文本区域的简单应用程序。程序中设置了不同类型的滚动条,并通过监听器实现了文本变化时的响应。
23万+

被折叠的 条评论
为什么被折叠?



