The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Creating a GUI with JFC/Swing
Lesson: User Interfaces that Swing: A Quick Start Guide

Your First Swing Program

This is the first of several sections that teach Swing basics by looking at example code. This section examines the code for a simple program, HelloWorldSwing(in a .java source file). The examples in the following sections will become progressively more difficult as we introduce and explain more features.

Here's a snapshot of the HelloWorldSwing program:

The HelloWorldSwing application.

And here's the code for HelloWorldSwing:
import javax.swing.*;        

public class HelloWorldSwing {
    public static void main(String[] args) {
        JFrame frame = new JFrame("HelloWorldSwing");
        final JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
This is one of the simplest Swing applications you can write. It doesn't do much, but the code demonstrates the basic code in every Swing program:
  1. Import the pertinent packages.
  2. Set up a top-level container.
The first line imports the main Swing package:
import javax.swing.*;
This is the only package that HelloWorldSwing needs. However, most Swing programs also need to import two AWT packages:
import java.awt.*;
import java.awt.event.*;
These packages are required because Swing components use the AWT infrastructure, including the AWT event model. The event model governs how a component reacts to events, such as button clicks and mouse motion. You'll learn more about events in the upcoming section Handling Events(in the Creating a GUI with JFC/Swing trail).

Every program with a Swing GUI must contain at least one top-level Swing container. A top-level Swing container provides the support that Swing components need to perform their painting and event handling. There are three top-level Swing containers: JFrame, JDialog, and (for applets) JApplet. Each JFrame object implements a single main window, and each JDialog implements a secondary window (a window that's dependent on another window). Each JApplet object implements an applet's display area within a browser window.

The HelloWorldSwing example has only one top-level container, a JFrame. A frame, implemented as an instance of the JFrame class, is a window that has decorations, such as a border, a title, and buttons for iconifying and closing the window. Applications with a GUI typically use at least one frame.

Here is the code that sets up and shows the frame:

JFrame frame = new JFrame("HelloWorldSwing");
...
frame.pack();
frame.setVisible(true);
HelloWorldSwing also has one component, a label that reads "Hello World." These two lines of code construct and then add the component to the frame:
final JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
To close the window when the close button close is clicked, we include this code in our HelloWorldSwing program:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFrame provides the setDefaultCloseOperation method to configure the default action for when the user clicks the close button. For single-window applications, most likely you want the application to exit. The EXIT_ON_CLOSE constant lets you specify this, as of version 1.3 of the Java 2 Platform. If you're using an earlier version of the platform, you implement an event listener to exit when the window closes:
frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
});
The next example will go into more details on event listeners.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.