Sams Teach Yourself C in 21 Days

(singke) #1
The first 4 lines of the program should require no explanation. Line 6 creates two
typeButtonvariables that will be used to refer to the program’s two buttons.
Line 7 creates an instance of the PopUpWindowclass, passing it the text to be displayed as
the window title. When this line is executed the pop-up window is created, and its con-
structor is executed, but the window is not displayed yet. Line 8 creates a layout manager
object to use when adding components to the main window.
Lines 10 to 18 are the constructor method for the program. Line 11 uses superto pass
the window title to the superclass. Line 12 associates the layout manager (that was cre-
ated in line 8) with this class’s window. Line 13 sets the initial window size. Lines 14 to
17 create two buttons (labeled “Show pop-up window” and “Quit program”) and add
them to the window.
Lines 19 to 22 are the program’s mainmethod. This method is made up of only two lines
of code. Line 20 creates an instance of the program, and line 21 makes it visible on the
screen.
Lines 24 to 36 are the actionmethod for this program. First, line 25 checks to see if
whatever event happened occurred to a Buttonobject. If so, line 26 retrieves the label on
the button that was clicked and puts it in the variable label. If the label indicates that the
“Show pop-up window” button was clicked, the code in line 28 checks whether the pop-
up window is already displayed. If so, it does nothing. If not, line 29 displays the pop-up
window. If the Quit button was clicked, line 35 exits the program.
You can see that in a relatively small program, Java lets you implement fairly sophisti-
cated programming including push buttons, windows, and event detection. There is a lot
more to the Java AWT, and I hope this brief sampling has whetted your appetite for
more!

Programming Java Applets ................................................................................


So far, the discussion of Java to programming has focused on stand-alone applications.
Another use of Java, a very popular use in fact, is to program applets, small programs
that are designed to be deployed on the World Wide Web and run in a Web browser such
as Netscape Navigator or Microsoft Internet Explorer. A Java applet is designed to be
part of a Web page. When you “surf” to a Web site that uses Java applets, the applets
simply appear in the browser as part of the page. Applets can be used for a wide variety
of tasks such as displaying animations, performing calculations, or just about anything
the programmer can think up.

758 Bonus Day 6

ANALYSIS

41 448201x-Bonus6 8/13/02 11:23 AM Page 758

Free download pdf