
A Simple Graphical User Interface
Example 2-1 is a simple program that uses Swing to create and display a graphical
user interface. Figure 2-1 shows the GUI created by this program.
Figur e 2−1: The GUI of the DisplayMessage program
The
DisplayMessage pr ogram is designed for use in a shell script or batch file.
*
If
you invoke the program on the command line with the text of a question, it dis-
plays the question to the user and waits for the user to click the Yes button or the
No button. The program sets its exit code based on the user’s response, so a shell
script can examine this exit code to determine how the user responded. The pro-
gram expects from one to three command-line arguments that specify the text of
the question and the text for the “Yes” and “No” buttons, respectively. For exam-
ple, you might invoke the program like this:
% java DisplayMessage "Do you really want to quit?" "Yes, Please", "No, Thanks"
The example illustrates step-by-step how to create a Swing GUI. Don’t worry if
you don’t understand the details of this program yet. If you read it through once
now to get the big picture, you can refer back to it as you read the sections that
follow.
Example 2−1: Cr eating a Simple GUI with Swing
import java.awt.*; // AWT classes
import javax.swing.*; // Swing components and classes
import javax.swing.border.*; // Borders for Swing components
import java.awt.event.*; // Basic event handling ...