Sams Teach Yourself C in 21 Days

(singke) #1
LISTINGB6.5 ThePopUpWindowclass is defined in PopUpWindow.java
1: import java.awt.*;
2:
3: class PopUpWindow extends Frame {
4:
5: FlowLayout lm = new FlowLayout(FlowLayout.CENTER);
6:
7: public PopUpWindow(String title) {
8: super(title);
9: setSize(300, 100);
10: setLayout(lm);
11: Button b = new Button(“Close”);
12: add(b);
13: }
14:
15: public boolean action(Event evt, Object arg) {
16: hide();
17: return true;
18: }
19: }

Line 3 defines the class PopUpWindowas a subclass of Frame. Line 5 creates a
FlowLayoutobject using the CENTERargument, which specifies that the layout
manager must center components in the window as much as possible. Lines 7 to 13 form
the class constructor that receives a type Stringargument specifying the window title.
Line 8 uses the superkeyword to pass the window title to the superclass. Line 8 defines
the pop-up window’s size, and line 10 specifies that the previously created layout man-
ager should be used when arranging components. Lines 11 and 12 create a new Button
object with the caption “Close”, and add it to the window.
Lines 15 to 18 define an event handler method for the class. Because there is only one
possible event for this class—the user clicking the Close button—there is no need to
write code to determine which event occurred. This event handler simply calls the hide
method to hide the window and then returns trueindicating the event has been handled
within the method.

LISTINGB6.6 This program PopUpDemo.java demonstrates the PopUpWindowclass and
user events
1: import java.lang.System.*;
2: import java.awt.*;
3:
4: public class PopUpWindowDemo extends Frame {
5:
6: Button open, quit;

756 Bonus Day 6

ANALYSIS

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

Free download pdf