Component Programming:

AWT Version

This example applet will serve as the basis for this discussion of auto-generated Swing code. It is the same program that was assigned as the first Java exercise.

It includes two buttons and two textfields. One button copies the text entered in one textfield into the second; the other button clears both textfields.

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

class TextCopy extends Applet implements ActionListener{

	Button clear, display;
	TextField input, output;
	Panel buttonPanel;  // hold two buttons

	public TextCopy ( )  {  // constructor

		this.setFont ( new Font ( "Helvetica", Font.PLAIN, 18 ) );

		display = new Button ( "display" );
		display.setBackground ( Color.red );
		display.addActionListener ( this );

		clear = new Button ( "clear" );
		clear.setBackground ( Color.green );
		clear.addActionListener ( this );

		input= new TextField ("type message here", 30);
		output = new TextField ("message display area", 30);
		output.setEditable ( false );
		output.setBackground ( Color.yellow );

		buttonPanel = new Panel();
		buttonPanel.setLayout ( new FlowLayout () );
		buttonPanel.add ( display );
		buttonPanel.add ( clear );


		setLayout ( new GridLayout ( 3, 1 ) );
		add ( buttonPanel );
		add ( input);
		add ( output );

	}  // end MyPanelColorS constructor

	public void actionPerformed (ActionEvent e) {

		if ( e.getSource() == display ) {
			output.setText ( input.getText() );
			return;
		}  // end display

		if ( e.getSource() == clear ) {
			input.setText ( "" );
			output.setText ( "" );
			return;
		}  // end display

	}  // end action

}  // end TextCopy


Run the applet