Repeater

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

  • How many components does the frame contain? Four:
    • The label "Enter Your Name:"
    • The text field that follows it
    • The label "Here is Your Name:"
    • The text field that follows it.
  • Which of them need a listener? Only the text field that the user types into.

Repeater

The labels do not need listeners because they generate no actions. The bottom text field does not need a listener because it will have its text set by the program (this is not an event.) Here is the application. Decide what should go in place of the ten buttons:

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

public class Repeater extends JFrame implements ActionListener
{

   JLabel inLabel     = new  (" " ) ;
   JTextField inText  = new JTextField( 15 );

   JLabel outLabel     = new  (" " ) ;
   JTextField outText = new JTextField( 15 );
   
   Repeater( String title )  // constructor
   {  
      super( title );
      setLayout( new FlowLayout() );        
      add(   ) 
      add(   ) ;
      add(   ) ;
      add(   ) ;

      inText.addActionListener( ) ;
      setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
   }

  public void actionPerformed( ActionEvent evt)  
  {
    String name = inText.getText();
    outText.( name );
    repaint();                  
  }

  public static void main ( String[] args )
  {
    . . . . . .
  }
}

In this program, the labels and text fields are constructed in the declartions outside of the constructor for the Repeater class. (In previous programs, componenents were constructed inside the frame's constructor.) Both styles work fine.

QUESTION 10:

Decide what should go in each blank. Fill in the blanks by clicking on a button.