Java Reference
In-Depth Information
creating user interfaces and for painting graphics and images, as well as
container classes to add components such as Buttons, TextFields, and Labels. The
AWT's Graphics class is quite powerful, allowing you to create shapes and display
images.
The other imported package used in many interactive applets is the
java.awt.event package. The java.awt.event package provides interfaces and
classes for dealing with different types of events triggered by AWT components.
The java.awt.event package is not a subset of the java.awt package; rather,
it is a separate package enabling you to implement interfaces, such as the
ActionListener and the ItemListener.
As you learned in Chapter 3, ActionListener listens for events that occur dur-
ing execution of the program, such as a user clicking a mouse button or pressing
the ENTER key. ItemListener can be added to an applet to listen for when the user
clicks components such as check boxes. ItemListener has several methods, such
as addItemListener() and itemStateChanged(), that enable you to test whether or
not items in the user interface are selected. These packages — java.awt,
java.applet, and java.awt.event, as well as the java.text.DecimalFormat used in
the Commission application — can be imported in any order.
Recall that applets do not have a main() method. Instead, applets use the
init() method to initialize the applet from the browser or Applet Viewer. When
the applet is loaded, the browser calls the init() method. This method is called
only once, no matter how many times you might return to the Web page.
Stubbing in the program will involve typing the general block comments;
importing the four classes; and entering the class header, the init() method
header, and the itemStateChanged() method header, as shown in Figure 4-37.
1 /*
2
Chapter 4: Sales Commission
3
Programmer: J. Starks
4
Date:
October 25, 2007
5
Filename:
CommissionApplet.java
6
Purpose:
This applet calculates sales commission using a sales amount
7
(input by the user) and a sales code (chosen from among option buttons).
8 */
9
10 import java.awt.*;
11 import java.applet.*;
12 import java.awt.event.*;
13 import java.text. DecimalFormat ;
14
15 public class CommissionApplet extends Applet implements ItemListener
16 {
17
public void init ()
18
{
19
}
20
21
//This method is triggered by the user clicking an option button
22
public void itemStateChanged ( ItemEvent choice )
23
{
24
}
25 }
FIGURE 4-37
Search WWH ::




Custom Search