The complete program 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
is registered —
for the text field the user types into.
(The user can type into the other field and can generate events
by hitting enter, but those events are ignored.)
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( String title ) // constructor { super( title ); setLayout( new FlowLayout() ); add( inLabel ) ; add( inText ) ; add( outLabel ) ; add( outText ) ; inText.addActionListener( this ); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public void actionPerformed( ActionEvent evt) { String name = inText.getText(); outText.setText( name ); repaint(); } public static void main ( String[] args ) { Repeater echo = new Repeater( "Repeater" ) ; echo.setSize( 300, 100 ); echo.setVisible( true ); } }
This program has all three parts of a GUI application: components in a container, a listener, and application code. The application code is there, although only a few statements long.
What statements in the program count as the application code?