The Program Explained

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:

It creates an object of the class JFrame and gives the frame a title.

The Program Explained

When you create a JFrame object it is not automatically displayed. A large application might create several JFrame objects, but might only display one of them at a time. Here is the program again:

import java.awt.*;    // 1. import the application windowing toolkit
import javax.swing.*; // 2. import the Swing classes. Note the "x" in javax.

public class TestFrame1
{
  // usual main() method 
  public static void main ( String[] args ) 
  {
    JFrame frame                        // 4. a reference to the object
        = new JFrame("Test Frame 1");   // 3. construct a JFrame object
    frame.setSize(200,100);             // 5. set it to 200 pixels wide by 100 high
    frame.setVisible( true );           // 6. ask it to become visible on the screen
    frame.setDefaultCloseOperation
        ( JFrame.EXIT_ON_CLOSE );       // 7. say what the close button does
  }
}

Here is an explanation of the program:

  1. The AWT is imported since it defines many classes that GUIs use.
    • For most programs you must do this.
  2. The Swing package is imported since that is where JFrame is defined.
    • Notice the 'x' in javax.
  3. A JFrame object is constructed.
  4. The reference variable frame refers to the JFrame object.
  5. The frame's setSize() method sets its size.
  6. The frame's setVisible() method makes it appear on the screen.
  7. The frame's setDefaultCloseOperation() sets what the "close" button does.

QUESTION 5:

When the program is running:

  • How many objects are there?
  • Where is the main() method?