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. [3] The examples in the following sections will become progressively more difficult as we introduce and explain more features. [3] HelloWorldSwing.java is included on the CD and is available online. See Code Samples (page 390). |
Here's a snapshot of the HelloWorldSwing program (Figure 96).
Figure 96. 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:
- Import the pertinent packages.
- 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 Handling Events (page 358).
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. [1]
[1] Swing applets are covered in Appendix B.
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
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: [1]
[1] To learn about your other options for window-closing events, see the chapter "How to Write a Window Listener" in the book The JFC Swing Tutorial. This chapter is also available on this book's CD and online at: http://java.sun.com/docs/books/tutorial/uiswing/events/windowlistener.html
frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
The next example will go into more details on event listeners.