Runnable Repeater Application
Be sure that you added the components in the correct order;
otherwise the GUI will not come out correctly.
Also notice that only one ActionListener was registered---the one
for the only TextField that generates an action.
import java.awt.*;
import java.awt.event.*;
public class Repeater extends Frame implements ActionListener
{
Label inLabel = new Label( "Enter Your Name: " ) ;
TextField inText = new TextField( 15 );
Label outLabel = new Label( "Here is Your Name :" ) ;
TextField outText = new TextField( 15 );
Repeater() // constructor
{
setLayout( new FlowLayout() ); // choose the layout manager
add( inLabel ) ;
add( inText ) ;
add( outLabel ) ;
add( outText ) ;
inText.addActionListener( this );
}
public void actionPerformed( ActionEvent evt)
{
String name = inText.getText();
outText.setText( name );
repaint();
}
public static void main ( String[] args )
{
Repeater echo = new Repeater() ;
WindowQuitter wquit = new WindowQuitter();
echo.addWindowListener( wquit );
echo.setSize( 300, 100 );
echo.setVisible( true );
}
}
class WindowQuitter extends WindowAdapter
{
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
|
This program has all three parts of a GUI application:
components in a container, a listener, and application code.
The application code is there,
but tiny.
If you want to see the GUI again
click here.
|