|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Using the Super() MethodIn Java terminology, the existing class is called the superclass. The class derived or extended from the superclass is called the subclass. Sometimes a superclass is referred to as a parent class, or a base class, and a subclass is referred to as a child class, an extending class, or a derived class. You can reuse or change the methods of superclasses. You can add new data and new methods in the subclasses. Subclasses usually have more functionality than their superclasses. The super() method refers to the superclass of a class in which the super() method appears. This method can be used in two ways: to call a superclass constructor or to call a superclass method. Calling Superclass ConstructorsThe syntax to call a superclass constructor is as follows: super( parameters ); In the BetterShape class, for example, the super() method can be used to call the Shape classs constructor. Anytime you use the super() method in the constructor, it has to appear in the first line of the constructor, and it is the only way to invoke a superclasss constructor.
Calling Superclass MethodsThe keyword super can be used to reference a method other than the constructor in the superclass. The syntax can look like this: super.method( parameters ); It is not always necessary to use the super keyword if youre calling one of the superclasss methods. If the subclass does not have a method matching the same name, all you have to do is call the superclass method you want to use. If, however, both the superclass and the subclass have methods of the same name, dont worry. From within the subclass method (or anywhere, really), you can make a call to the superclasss method by inputting super.method() as shown in the preceding example. Calling with the this KeywordThe keyword super is used to reference superclasses. Occasionally, you need to reference the current class. Java provides another keyword, this, for referencing the current object. Use of the this keyword is analogous to use of super. For instance, in the preceding section we talked about calling super.method. You can also call this.method(), or you can access this.method() to call methods in the current class. The following syntax line shows how to use the this keyword to call a method in the current class: this.method( parameters ); Creating a Program That Uses an Extended Frame ClassWere going to keep things simple! Were going to create an applet that declares a class called MyFrame. The MyFrame class is extended from the Frame class. It doesnt do anything more than add its own paint() method. You can use this paint() method, then, to draw anything you want in the Frame window. The source-code listing for this applet is given in Listing 5.3. You can see the applet running in Figure 5.4. Note that if you look at the figure, youll see two windows: the Main applet window and the Frame window, which is above the applet window. Listing 5.3 The MyFrame Applet 1 import java.awt.*; 2 import java.applet.*; 3 4 public class Applet1 extends Applet 5 { 6 // Declare and create the MyFrame object. 7 MyFrame m_MyFrame = new MyFrame( "My Frame" ); 8 9 public void init() 10 { 11 // Set the size of the window. 12 m_MyFrame.setSize( 200, 200 ); 13 // Make the window visible. 14 m_MyFrame.setVisible( true ); 15 } 16 17 public void paint( Graphics g ) 18 { 19 // Draw to the applet window. 20 g.drawString( "Drawing to the applet", 20, 20 ); 21 } 22 // Declare a new class that extends the Frame class. 23 public class MyFrame extends Frame 24 { 25 public MyFrame( String s ) 26 { 27 // Call the Frame constructor. 28 super( s ); 29 } 30 31 public void paint( Graphics g ) 32 { 33 // Draw to the Frame window. 34 g.drawString( "Drawing to the frame", 20, 50 ); 35 } 36 37 } 38 39 }
Listening for Frame Class EventsEarlier today, we talked about the problem of the Frame window not closing when you clicked the Close button. To respond to events such as the user clicking the Close button, we must create a class inside of the Frame class that will listen for such events. The class we create will extend the WindowAdapter class. (And you were wondering why we spent time today talking about classes and how to extend them!) After we declare this, we can override the windowClosing() method and do whatever we want, which is to close our window. Now the last step! Inside of the Frame classs constructor, call the addWindowListener() method and add the extended WindowAdapter class. These are the steps necessary to listen for frame window events:
Seven different events are available to you when you have a class thats extended from the WindowAdapter class. They are windowActivated(), windowClosed(), windowClosing(), windowDeactivated(), windowDeiconified(), windowIconified(), and windowOpening() methods. Table 5.1 shows you all these methods.
You need to remember one more thing when youre trying to get events from Frame windows. You need to import Java.AWT.event (line 3 in Listing 5.4). To create the example in Listing 5.4, I simply took the preceding example and followed the three steps for event handling. Notice that in line 3, I imported Java.AWT.event. Also notice that in line 32, I made a call to the addWindowListener() method. This is what actually adds the WindowListening class to the event cue. Now look at line 42. This is where my WindowListening class begins. The only method Im listening for is the windowClosing() method, which starts at line 45. You can see at line 48 that the windows visibility is set to false, which hides it. Then the applet sets the parent classs m_MyFrame variable to null. Now, when you click the Frame windows Close button, it will actually close.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |