Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Listing 5.4 The MyFrame Applet Incorporates the Three Steps for Handling Events

1   import java.awt.*;
2   import java.applet.*;
3   import java.awt.event.*;
4
5   public class Applet1 extends Applet
6   {
7       // Declare and create the MyFrame object.
8       MyFrame m_MyFrame = new MyFrame( "My Frame" );
9
10      public void init()
11      {
12          // Set the size of the window.
13          m_MyFrame.setSize( 200, 200 );
14          // Make the window visible.
15          m_MyFrame.setVisible( true );
16      }
17
18      public void paint( Graphics g )
19      {
20          // Draw to the applet window.
21          g.drawString( "Drawing to the applet", 20, 20 );
22      }
23
24      // Declare a new class that extends the Frame class.
25      public class MyFrame extends Frame
26      {
27          public MyFrame( String s )
28          {
29              // Call the Frame constructor.
30              super( s );
31
32              addWindowListener( new WL() );
33          }
34
35          public void paint( Graphics g )
36          {
37              // Draw to the Frame window.
38              g.drawString( "Drawing to the frame", 20, 50 );
39          }
40
41          // Our window listener class.
42          public class WL extends WindowAdapter
43          {
44              // Override the windowClosing() method.
45              public void windowClosing( WindowEvent e )
46              {
47                  // Hide the window.
48                  setVisible( false );
49                  // Set the parent class to null.
50                  m_MyFrame = null;
51              }
52          }
53
54      }
55
56  }
The Import Statement

The import statement tells the compiler to include existing Java programs in the current program. In the case of the preceding applet, we imported Java.AWT, Java.applet, and Java.AWT.event. You can use the operations in Java.AWT.event in your program instead of rewriting the code. This is an example of software reusability; that is, the program is written once and is used by many other people without being rewritten.

Java code is organized into packages and classes. Classes are inside packages, and packages are libraries of Java code that contain all kinds of operations ready for you to import and use. Java provides standard libraries such as Java.AWT that come with a compiler.

Collecting the Garbage

Previous languages such as Lisp and Smalltalk implemented their languages in such a way that programmers could ignore memory deallocation. The developers of languages such as Lisp and Smalltalk felt that the language should be able to determine what is no longer useful, and get rid of it. In relative obscurity, these pioneering programmers developed a whole series of garbage collectors to perform this job, each getting more sophisticated and efficient as the years went by.

Finally, now that the mainstream programming community has begun to recognize the value of this automated technique, Java can become the first really widespread application from the technology these pioneers developed. Imagine that you’re a programmer in a C-like language. Each time you create something dynamically in such a language, you’re completely responsible for tracking the life of that object throughout your program, and mentally deciding when it will be safe to deallocate it. This can be quite a difficult task, because any of the other libraries or methods you called might have squirreled away a pointer to the object, unbeknownst to you. When it becomes impossible to know, you simply choose never to deallocate the object, or at least wait until every library and method call involved has completed, which can be nearly as long.

The uneasy feeling you get when writing such code is a natural, healthy response to what is inherently an unsafe and unreliable style of programming. If you have tremendous discipline, you can (in principal) survive this responsibility without too many mishaps, but memory problems are the leading cause of software failure and software bugs today. That’s why companies such as NuMega, who published BoundsChecker, make so much money. Their products help programmers track down these memory allocation errors.


Previous Table of Contents Next


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.