// Demonstrate text field. (Java 1.1 or later version)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.String.*;
import java.lang.Double.*;

public class TextFieldDemo extends Applet implements ActionListener
{

   TextField value1Field, value2Field;
   String message = ""; // String containing system messages
   String value1Text;
   double value1, value2;
   double value3;

   public void init()
   {
	Label value1Label = new Label( "Value 1: ", Label.RIGHT );
	Label value2Label = new Label( "Value 2: ", Label.RIGHT );
	value1Field = new TextField( 12 );
	value2Field = new TextField( 12 );

	// Add controls
	add( value1Label );
	add( value1Field );
	add( value2Label );
	add( value2Field );

	// Register ActionListeners to receive action events.
	value1Field.addActionListener( this );
	value2Field.addActionListener( this );
   }

   // ActionListeners.
   public void actionPerformed( ActionEvent ae )
   {
	repaint();
   }

   public void paint( Graphics g )
   {
	g.drawString( "Value 1: " + value1Field.getText(), 6 ,60 );
	g.drawString( "Selected text in value1: " + value1Field.getSelectedText(), 6, 80 );
	g.drawString( "Value 2: " + value2Field.getText(), 6, 100 );

	message = "";

	try
	{
		value1Text = value1Field.getText();
		value1 = (double)Long.parseLong( (String)value1Text );
	}
	catch( NumberFormatException exceptionText )
	{
		message = "\"value1\" field must contain a number: " + exceptionText;
		value1 = 0.0;
	}
	try
	{
		value2 = (double)Long.parseLong( value2Field.getText() );	
	}
	catch( NumberFormatException exceptionText )
	{
		message = "\"value2\" field must contain a number: " + exceptionText;
		value2 = 0.0;
	}

	value3 = value1 + value2;
	g.drawString( "Value 1 + value 2 = " + value3, 6, 120 );


	g.drawString( "Message: " + message, 6, 180 );
   }

   // main() is the entry point when called as an application.
   // main() is not used when run as an applet.
   public static void main ( String args[] )
   {
	Frame mainWindow = new Frame ( "Text Field Demo" );
	TextFieldDemo application = new TextFieldDemo ();

	mainWindow.setSize ( 200, 200 );
	mainWindow.add ( "Center", application );
	mainWindow.show ();
	application.init();
   }

}
