Here is a simple program which opens up a graphics window:
// 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:
The phrase implements Runnable declares that the program
object has a public method called run.
The invokeLater method makes sure that all program
initialisation is complete before the run method in the program
object is called.
The setSize method is used here because the frame has
nothing in it. It is usually better to let the frame size itself using
pack according to the things it contains.
The setTitle method affects what appears in the title
bar.
The setDefaultCloseOperation method affects what happens
when you press the close button in the title bar. The default is to close the
window, but not shut down the application (which might have other windows
open). For a one-window application, you have to make this call, or you may be
left with a phantom running program.
The setLocationByPlatform method is called just before
setVisible so that the platform which the program is runing on
chooses where to place the frame according to its own conventions, instead of
placing it in the default top left corner of the screen.
The setVisible method makes the window appear on the
screen. It doesn't appear before that, so that you can make sure everything is
in place before the user sees the window and starts interacting with
it.
The class is called JFrame rather than just
Frame because Java has an old graphics library (awt) and a new
graphics library (swing) added to provide more platform independence. To avoid
name clashes, most of the classes in the new library begin with
J. Not everything has been replaced, so most graphics programs
need to use both libraries.
The program does not terminate, even though the code reaches the end of
the main() method. This is because a separate thread is created to
handle the graphics, which runs indefinitely.