Chapter 3 Introduction (Web page design) to Java Applets 125 1

Chapter 3 Introduction to Java Applets 125 1 2 3 4 Fig. 3.13 AdditionApplet.htmlloads class AdditionAppletof Fig. 3.12 into the appletviewer. Lines 1 2 // Fig. 3.12: AdditionApplet.java // Adding two floating-point numbers. are single-line comments stating the figure number, file name and purpose of the program. Line 5 import java.awt.Graphics; // import class Graphics imports class Graphics (package java.awt) for use in this applet. Actually, the import statement at line 5 is not required if we always use the complete name of class Graphics java.awt.Graphics which includes the full package name and class name. For example, the first line of method paintcan be defined as public void paint( java.awt.Graphics g ) Software Engineering Observation 3.3 The Java compiler does not require import statements in a Java source code file if the complete class name the full package name and class name (e.g., java.awt.Graphics) is specified every time a class name is used in the source code. Line 8 import javax.swing.*; // import package javax.swing specifies to the compiler that several classes from package javax.swingare used in this applet. The asterisk (*) indicates that all classes in the javax.swing package (such as JApplet and JOptionPane) should be available to the compiler so the compiler can ensure that we use the classes correctly. This allows programmers to use the shorthand name (the class name by itself) of any class from the javax.swingpackage in the program. Remember that our last two programs imported only class JAppletfrom package javax.swing. In this program, we use classes JAppletand JOptionPanefrom that package. Importing an entire package into a program is also a shorthand notation so the programmer is not required to provide a separate importstatement for every class used from that package. Remember that you can always use the complete name of every class, i.e., javax.swing.JApplet and javax.swing.JOptionPane rather than import statements. Software Engineering Observation 3.4 The compiler does not load every class in a package when it encounters an import statement that uses the * (e.g., javax.swing.*) notation. The compiler loads from the package only those classes the program uses. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply