The JLabel Class

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:

JLabel

The JLabel Class

Here is a constructor for JLabel:

JLabel( "Some Words" )

It constructs an object which can be added to a frame. Its size and location is managed by the layout manager. Here is our sample program:

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

public class TextEg2 extends JFrame
{

  JTextField text;
   lbl ;

  public TextEg2( String title )
  {  
     super( title );
     text = new JTextField( 15 ) ;
     
     lbl  = new  (  ); 
     
     setLayout( new FlowLayout() );
     
     add(  );
     add(  );
     
     setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
  }

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

With FlowLayout, components are put into the frame left to right, top to bottom in the order that they are added.

QUESTION 6:

Fill in the blanks so that the label "Enter Your Name" is placed to the left of the text field.