Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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
. 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:And here's the code for HelloWorldSwing
: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 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); } }The first line imports the main Swing package:
- Import the pertinent packages.
- Set up a top-level container.
This is the only package thatimport javax.swing.*;HelloWorldSwing
needs. However, most Swing programs also need to import two AWT packages: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.import java.awt.*; import java.awt.event.*;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
. EachJFrame
object implements a single main window, and eachJDialog
implements a secondary window (a window that's dependent on another window). EachJApplet
object implements an applet's display area within a browser window.The
HelloWorldSwing
example has only one top-level container, aJFrame
. A frame, implemented as an instance of theJFrame
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:To close the window when the close button is clicked, we include this code in ourfinal JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label);HelloWorldSwing
program:frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JFrame
provides thesetDefaultCloseOperation
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. TheEXIT_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:The next example will go into more details on event listeners.frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.