The complete program (suitable for copying and pasting to NotePad) is given below:
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 text field that generates an action.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Repeater extends JFrame implements ActionListener
{
JLabel inLabel = new JLabel( "Enter Your Name: " ) ;
TextField inText = new TextField( 15 );
JLabel outLabel = new JLabel( "Here is Your Name :" ) ;
TextField outText = new TextField( 15 );
public Repeater() // constructor
{
getContentPane().setLayout( new FlowLayout() );
getContentPane().add( inLabel ) ;
getContentPane().add( inText ) ;
getContentPane().add( outLabel ) ;
getContentPane().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.