JFC Applications

   

In previous chapters, you were exposed to non-JFC applications (that is, Java applications that don't make use of graphics or GUIs). It's time to see what a JFC application looks like. To that end, you're going to have an opportunity to examine a pair of AWT and Swing skeletal JFC applications.

A Taste of JFC's AWT Applications

An AWT application uses the AWT API. This application contains a class that either inherits from the AWT's Frame class or creates an instance of the Frame class in its main method.

Note

A frame is a window that provides a title bar, window decorations (such as a system menu, minimize and maximize buttons , borders, and so on), a menu bar, and an area to place GUI components .


An AWT application often incorporates a skeletal structure that looks similar to the code in Listing 12.1.

Listing 12.1 The AppAWTSkeleton Application Source Code
 // AppAWTSkeleton.java import java.awt.*; import java.awt.event.*; class AppAWTSkeleton extends Frame {    AppAWTSkeleton (String title)    {       super (title);       addWindowListener (new WindowAdapter ()                          {                              public void windowClosing (WindowEvent e)                              {                                 System.exit (0);                              }                          });       // Build GUI here.       pack ();       setVisible (true);    }    public static void main (String [] args)    {       new AppAWTSkeleton ("Title goes here");    } } 

As Listing 12.1 shows, this AWT application's sole class extends the Frame class. Its main method creates an instance of this class and invokes the constructor. A title is passed to this constructor, which is further passed to one of Frame 's constructors, via the super (title) method call. This title will be displayed in the frame's title bar.

After taking care of the title, the constructor registers a window listener that responds to window closing events. To register this listener, the addWindowListener method is called with a reference to an object created from an anonymous subclass of the WindowAdapter class. In this subclass, the windowClosing method is overridden to allow the application to exit when the user selects the Close box from the frame. (Behind the scenes, this method is called when the user selects the frame's Close box.)

Now that the constructor has taken care of the frame's title and window closure registration, it can proceed to construct a GUI. Once the GUI has been built, the pack method is called to establish the size of the frame (by determining the preferred sizes of all contained components). Finally, the setVisible method is called, with a Boolean true value argument, to ensure that the frame is visible.

Tip

As an alternative to pack, a program can also call the setSize method to explicitly specify the frame's horizontal and vertical sizes.


The constructor exits, along with the main thread. However, the program does not exit because background threads that manage the frame are still running.

To get an idea of what an AWT application's output looks like, take a look at Figure 12.1. This figure illustrates the output from the PNViewer1 AWT application. (Although not shown in this chapter, PNViewer1 's source code is included with the rest of this book's source code.)

Figure 12.1. The PNViewer1 application presents an AWT GUI consisting of a planetary nebula image (displayed in a custom Picture component) and a Choice component.

graphics/12fig01.gif

Note

Planetary nebulae are gaseous clouds surrounding those stars that shed their outer layers through violent explosions. When viewing an image of a planetary nebula, keep in mind that the star located at the center of the nebula is the source of the explosions that formed the nebula. (The star still exists: it just "hiccupped.")


If you study PNViewer1 's source code, you'll find that there is a lot of code required to construct this application. However, most of this code is involved in building the GUI. You might also notice a similarity between PNViewer1 's skeletal structure and AppAWTSkeleton 's structure. This is no accident : I tend to use this skeletal structure in many of my AWT applications.

You'll get a chance to study the various elements that contribute to PNViewer1 and other AWT applications when you get to Chapters 13, "Beginning AWT," and 14, "Building a GUI: AWT Style."

Troubleshooting Tip

Suppose you want to create an AWT application that generates an audible beep when something important happens. You don't want to go to all the bother of loading and playing a sound file. (In fact, you don't want to have to distribute a sound file with your program.) Is there a simple way to generate this beep? The answer is yes. To see how this is done, check out "Beeping the System Speaker" in the "Troubleshooting" section at the end of this chapter.


A Taste of JFC's Swing Applications

In contrast to AWT applications, a Swing application uses the Swing API. It can also take advantage of the AWT API, although discernment is required in choosing what portions of this API to use.

Each Swing application contains a class that either inherits from Swing's JFrame class or creates an instance of the JFrame class in its main method. Swing applications incorporate skeletal structures that look similar to the code in Listing 12.2.

Listing 12.2 The AppSwingSkeleton Application Source Code
 // AppSwingSkeleton.java import javax.swing.*; import java.awt.event.*; class AppSwingSkeleton extends JFrame {    AppSwingSkeleton (String title)    {       super (title);       setDefaultCloseOperation (EXIT_ON_CLOSE);       // Build GUI here.       pack ();       setVisible (true);    }    public static void main (String [] args)    {       new AppSwingSkeleton ("Title goes here");    } } 

As Listing 12.2 shows, a Swing application's skeletal structure is similar to the skeletal structure of an AWT application. However, there are differences ”besides extending JFrame instead of Frame.

The first difference can be found in the imports section. Whereas an AWT application contains an import java.awt.*; import, a Swing application contains an import javax.swing.*; import. Although not required in AppAWTSkeleton and AppSwingSkeleton, both AWT and Swing applications often contain an import java.awt.event.*; import, so they can register objects that listen for various events.

The second difference is the manner by which a window listener is registered to listen for window closing events. Unlike AWT applications, a Swing application doesn't need to call Frame 's addWindowListener method with an argument that references an object created from an anonymous subclass of the WindowAdapter class. Instead, a Swing application calls JFrame 's setDefaultCloseOperation method with an EXIT_ON_CLOSE argument to register this intent.

To get an idea of what a Swing application's output looks like, take a look at Figure 12.2. This figure illustrates the output from the PNViewer2 Swing application. (Although not shown in this chapter, PNViewer2 's source code is included with the rest of this book's source code.)

Figure 12.2. The PNViewer2 application presents a Swing GUI consisting of an image (displayed in a JLabel component) and a JComboBox component.

graphics/12fig02.gif

For those who study PNViewer2 's source code, you'll notice that it was designed to be as similar to the PNViewer1 source code as possible (for comparison purposes). Once you've learned about Swing, you'll be in a good position to understand the differences between these two programs.

You'll get a chance to study the various elements that contribute to PNViewer2 and other Swing applications when you get to Chapters 15, "And Then There Was Swing" , and 16, "Building a GUI: Swing Style" .

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net