Page 55 - Introduction to Programming with Java: A Problem Solving Approach
P. 55
shapes. For readers who do not have time for the GUI track, no problem. Any or all of the GUI track sec- tions may be skipped as they cover material that is independent of later material. Note that we cover hard- core GUI material in earnest at the end of the book in Chapters 16 and 17. The GUI material in Chapters 16 and 17 is independent of the GUI material in the GUI track, so, once again, it’s OK to skip the GUI track. But why skip it? GUI programming is sooooo much fun!
In this section, we present a GUI version of the Hello World program. We’ll start by showing you the program’s output:
A GUI program is defined as a program that uses graphical tools for its interface. This program is in-
deed a GUI program because it uses these graphical tools for its interface: a title bar (the bar at the top of
the window), a close-window button (the “X” in the top-right corner), an OK button, and an i icon. Here’s
how the tools work: If you drag the title bar with your mouse, the window moves. If you click the close-
window button or the OK button, the window closes. The i icon is a visual cue that indicates the nature of the
window—the i stands for “information” since the window simply displays information.
Go ahead and enter the program code into a text editor. If you need a refresher on how to do that, see the previous section. This time, save your source code file with the name HelloGUI.java instead of Hello.java. When saving HelloGUI.java, make sure you spell the filename with capitals for H, G, U, and I since that’s how HelloGUI is spelled in your program’s third line. Next, you’ll want to compile and run the program. Once again, if you need a refresher, see the previous section.
Apago PDF Enhancer
See Figure 1.11. The dashed boxes indicate code that differs from the code in the previous section’s Hello program. For now, don’t worry about the meaning of the program’s code. We’ll explain it later on. For now, the goal is to give you some fun and valuable hands-on experience.
1.9 GUI Track: Hello World (Optional) 21
import javax.swing.JOptionPane;
public class HelloGUI
The dashed boxes indicate code that differs from the code in the previous section’s Hello program.
{
}
public static void main(String[] args)
{
}
JOptionPane.showMessageDialog(null, "Hello, World!");
Figure 1.11 GUI version of the Hello World program