Course Content‎ > ‎Session 7‎ > ‎

Creating a Frame in Swing

A top-level window i.e. a window that is not contained inside another window
is called a frame in Java. The AWT library has a peer-based class called Frame
for this top level. The Swing version of this class is called JFrame. JFrame
extends the Frame class. Frames are examples of containers i.e. they can
contain other user interface elements. Let’s look at an example that displays
a simple empty frame on the screen:
    import javax.swing.*;
    class FirstFrame extends JFrame
        public FirstFrame()
            setTitle("First Frame");
            setSize(300, 200);
        public static void main(String[] args)
            JFrame Frame = new FirstFrame();
The Swing classes are placed in the javax.swing package. “javax” indicates the
Java extension package. In this example, we create a class called FirstFrame,
which behaves just like JFrame in the javax.swing package. The only difference
is that the constructor FirstFrame() sets the title to “First Frame” with the
size of the frame to 300 x 200 pixels. The main() method shows the frame and
exits. That just terminates the main thread not the program. Showing the window
activates a user interface thread that keeps the program alive. The steps
involved in showing the frame are:
    1. Create a frame object by a call to new.
         JFrame Frame = new FirstFrame();      
    2. In order to display the frame and bring it in front, you use the show()
       or setVisible() methods.
    3. In Windows, neither is the program terminated by selecting close from the
       File menu nor by clicking on the “X” in the upper right-hand corner. These
       options only hide the window. In UNIX, you can select “Destroy” from the
       system menu. For now, you can either go to Task Manager or use Ctrl + C
       to close the program.
    4. You can properly terminate the program, when the user closes the frame.
       The way this is accomplished varies between the AWT and Swing. For example,
       the AWT requires a window listener to be registered with the frame, and
       this listener's overridden windowClosing() method to call System.exit(),
       which terminates the application. Swing frame windows can use this
       technique, if so desired. However, they can also take advantage of JFrame's
       setDefaultCloseOperation() method and the EXIT_ON_CLOSE argument, to
       accomplish the same task.
The CloseFrameAWT application shows how a registered window listener is used
to close an AWT frame and terminate the application; and the CloseFrameSwing
application shows how setDefaultCloseOperation() accomplishes the same task.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class CloseFrameAWT extends Frame
        CloseFrameAWT (String title)
            // Assign title to frame window's title bar.
            super (title); 
            addWindowListener (new WindowAdapter ()      
                public void windowClosing (WindowEvent e)
                    System.exit (0);
            setSize (300, 200);
            setVisible (true);
        public static void main (String [] args)
            CloseFrameAWT frame = new CloseFrameAWT ("Close Frame AWT");