Text Echo Applet


//  this applet sets up a TextField where the user can type text. when the user
//  hits return the text is copied into the TextArea

import java.awt.*;
import java.awt.event.*;
public class TextEcho11 extends java.applet.Applet implements ActionListener
{
    private TextArea area;
    private TextField field;

//  use BorderLayout to arrange the text components in the applet
//  the TextArea in the center will be sized to fill the center of the
//  BorderLayout

    public void init() {
        setLayout( new BorderLayout() );
        add( "Center", area = new TextArea() );
        area.setFont( new Font("Serif",Font.BOLD,18) );
        area.setText("Howdy!\n");
        add( "South", field = new TextField() );
        field.addActionListener(this);
    }

//  this method will receive an event when the user hits the return key
//  and it will copy the text from the TextField to the TextArea

    public void actionPerformed(ActionEvent e)
    {
        area.append(field.getText() + '\n');
        field.setText("");
    }
}