/* * File: TextFieldTest.java * * Create an editable text field (i.e., a one-line text area) * * Copyright: Northeast Parallel Architectures Center * */ import java.applet.Applet; import java.awt.Label; import java.awt.TextField; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class TextFieldTest extends Applet { // label1 and label2 are labels for the two textfields username and password // when "enter" is pressed in either textfield, display the text in label3 Label label1, label2, label3; TextField username, password; private Font f = new Font ("Dialog", Font.BOLD, 30); public void init() { // this example shows how to use a separate class as the event listener TextFieldHandler handler = new TextFieldHandler( this ); setBackground( Color.white ); setFont (f); label1 = new Label( "Username:" ); username = new TextField( 20 ); username.addActionListener( handler ); label2 = new Label( "Password:" ); password = new TextField( 20 ); password.setEchoChar( '*' ); password.addActionListener( handler ); // this layout manager uses intial text to size the label label3 = new Label (" "); add( label1 ); add( username ); add( label2 ); add( password ); add( label3 ); } } class TextFieldHandler implements ActionListener { private TextFieldTest applet; public TextFieldHandler( TextFieldTest a ) { applet = a; } public void actionPerformed( ActionEvent event ) { String text; // respond to text enter for the two textfields by showing in label3 if ( applet.username == event.getSource() ) { text = applet.username.getText(); applet.label3.setText("Your username: " + text); } if ( applet.password == event.getSource() ) { text = applet.password.getText(); applet.label3.setText("Your password: " + text); // you can also display text in the browser status bar applet.showStatus( "Text: " + text ); } } }