JFrame

JFrame class is a Swing extension of the AWT Frame class (Swing adds a J).

It is one of the few ``non-painted'' graphics components.

Frames are examples of containers and we will study ways of inserting graphics objects into them.

Swing lives in javax.swing.*.

The 'x' stands for 'extension'.

setSize

By default JFrame has 0x0 pixels! You need to define your own frame class which extends it and which is bigger. Use the setSize() method to increase its size.
Units of size are pixels.

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

defines what happens when you close the frame

In other programs you would want to change this so that the program did not exit whenever the user closed a window. By default a frame is ``hidden'' when a user closes it but the program does not terminate.

show

Frames start their life invisible! You must call show to display them.
Note that the main program exits after the show call.
This just terminates the main thread.
Graphics is in a separate thread.

What else to do ?

Many of the useful JFrame methods are inherited from other classes further up the hierarchy

javax.swing
Class JFrame

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--java.awt.Window
                    |
                    +--java.awt.Frame
                          |
                          +--javax.swing.JFrame

Frame class

Component and Window classes
(particularly methods to resize and position the frame):

This method can move the frame around. Note that y measures the number of pixels down from the top left corner of the screen. Also note that both arguments are integers.

Note that the coordinates in JFrame methods are with respect to the whole screen. In other swing classes they are usually with respect to a specific container.

How to get Screen Dimension ?

It is possible to get the dimensions of the screen you are using by creating an object of class Toolkit and calling its getScreenSize() method. This returns the width and height as fields in a dimension object. The pattern is


        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();

        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;