go to previous page   go to home page   go to next page

Answer:

These two lines (might) count as the "application code:"

    String name = inText.getText();
    outText.setText( name );

(Although it could be argued that there is no application code because all the code deals with the GUI.)


Application Code in its own Method

In a typical application with a graphical interface, about 40% of the code manages the interface. The rest of the code is for the application — the reason the program was written. Usually the application code is kept separate from the code that manages the interface. In a big application it would be confusing to mix the two together. Here is our tiny application with a separate method for the application code:

import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;

public class Repeater extends JFrame implements ActionListener
{

   JLabel inLabel     = new JLabel( "Enter Your Name:  " ) ;
   JTextField inText  = new JTextField( 15 );

   JLabel outLabel    = new JLabel( "Here is Your Name :" ) ;
   JTextField outText = new JTextField( 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 );   
   }

  // The application code.
  void copyText()
  {
    String name = inText.getText();
    outText.setText( name );
  }

  public void actionPerformed( ActionEvent evt)  
  {
    copyText();
    repaint();                  
  }

  public static void main ( String[] args )
  {
    Repeater echo  = new Repeater( "Repeater" ) ;
    echo.setSize( 300, 100 );     
    echo.setVisible( true );      
  }
}

A large application would be divided into separate classes, some for the GUI and others for the application. Each class might be kept in its own file.


QUESTION 12:

When you enter text into the top JTextField and hit enter, the text is copied to the lower JTextField. What happends if you enter text into the lower JTextField and hit enter?