Sams Teach Yourself C in 21 Days

(singke) #1
12:
13: public static void main(String args[]) {
14: AWT_Test1 Test = new AWT_Test1(“A Java Window”);
15: Test.resize(350,250);
16: Test.show();
17: }
18: }

(see Figure B6.1)

750 Bonus Day 6

LISTINGB6.3 continued

OUTPUT

FIGUREB6.1
The output of the pro-
gram in Listing B6.3.

Line 1 of this program imports the classes in the AWT. Line 3 declares the class
(program)AWT_Test1to be a subclass of Frame. Lines 6 through 10 form the
class constructor, which is declared to take one type Stringargument that is the win-
dow’s title. Line 7 uses the superkeyword to pass the title to the superclass construc-
tor—in other words, the constructor method of the Frameclass (see the sidebar for more
onsuper). Other code in the constructor creates a new label object with the text “Hello
from Java”(line 8), defines the font and font size for the label (line 9), and adds the
label at the center of the window (line 10).
In the mainmethod, line 14 creates a variable named Testand initializes it with a new
instance of the class AWT_Test1. Creating a new instance calls the class constructor (lines
6 through 10), passing it the string “A Java window”which is then passed up to the
superclass constructor, as described above. Line 15 sets the window size to 350×250 pix-
els. Finally, line 16 displays the window onscreen.
You can see that Java AWT graphics are completely object based, which is in perfect
keeping with the object-oriented nature of Java itself.

ANALYSIS

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

Free download pdf