📜  Java AWT TextArea

📅  最后修改于: 2020-09-28 04:47:40             🧑  作者: Mango

Java AWT TextArea

TextArea类的对象是显示文本的多行区域。它允许编辑多行文本。它继承了TextComponent类。

AWT TextArea类声明

public class TextArea extends TextComponent

Java AWT TextArea示例

import java.awt.*;
public class TextAreaExample
{
 TextAreaExample(){
    Frame f= new Frame();
            TextArea area=new TextArea("Welcome to javatpoint");
    area.setBounds(10,30, 300,300);
    f.add(area);
    f.setSize(400,400);
    f.setLayout(null);
    f.setVisible(true);
 }
public static void main(String args[])
{
   new TextAreaExample();
}
}

输出:

带有ActionListener的Java AWT TextArea示例

import java.awt.*;
import java.awt.event.*;
public class TextAreaExample extends Frame implements ActionListener{
Label l1,l2;
TextArea area;
Button b;
TextAreaExample(){
l1=new Label();
l1.setBounds(50,50,100,30);
l2=new Label();
l2.setBounds(160,50,100,30);
area=new TextArea();
area.setBounds(20,100,300,300);
b=new Button("Count Words");
b.setBounds(100,400,100,30);
b.addActionListener(this);
add(l1);add(l2);add(area);add(b);
setSize(400,450);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String text=area.getText();
String words[]=text.split("\\s");
l1.setText("Words: "+words.length);
l2.setText("Characters: "+text.length());
}
public static void main(String[] args) {
new TextAreaExample();
}
}

输出: