xhtml
 

Frames

Here is a simple program which opens up a graphics window:

MainFrame.java
// Create a graphics window.

import javax.swing.*;

class MainFrame implements Runnable
{
   public static void main (String[] args)
   {
      MainFrame program = new MainFrame();
      SwingUtilities.invokeLater(program);
   }

   public void run()
   {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(400,300);
      frame.setTitle("An Empty Frame");
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }
}

The program, which we have chosen to call MainFrame, creates a JFrame which is the graphics class representing a top level window on the screen. The exact details of a top level window are platform dependent, because it interacts with the local window system. You can run the program to check out these details. Typically, there is a thin border which you can grab with the mouse and drag to change the size of the window, and a title bar which you can grab and drag to move the window around. The title bar also has a button in the top (right or left) corner for closing the window, and possibly other buttons, e.g. to maximize/minimize or iconify the window. A menu bar can also be added to the frame.  Other points to notice about this program are:


Back