Application with a Text Field

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:

The Java documentation from a current JDK distribution is the best place.

Application with a Text Field

GUI of the application

You may have this documentation on your computer already. If not, you can download it from Sun. Scan it to see how these notes relate to the official documentation. The purpose of these notes is to overview the Swing package. Look to the documentation for complete coverage.

Here is one (of several) constructors for JTextField. The parameter says how many characters wide the field is.

JTextField( int numChars ) 

Notice that the parameter is number of characters, not the size in pixels or other measure. This program displays the frame at top.

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

public class TextEg1 extends 
{

   text;

  // constructor for TextEg1
  public TextEg1()
  { 
    super( title ); 
    
    text = new  ;
    
    setLayout( new FlowLayout() );
    add( text );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
  }

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

}

QUESTION 4:

Fill in the blanks so that the field is 15 characters wide.