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 ); setDefaultCloseOperation( WindowConstants.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() ; 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, but tiny.
What lines in the program count as the application code?