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(400,300);             // 5. set it to 400 pixels wide by 300 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
  }
}

Explanation of the program:

  1. The AWT is imported since it defines many classes that GUIs use.
    • For most programs you must do this.
    • This program does not actually need this package.
  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: