MyButton is an inner class that extends Button. Its constructor uses super to pass the
label of the button to the superclass constructor. It calls enableEvents( ) so that action events
may be received by this object. When an action event is generated, processActionEvent( ) is
called. That method displays a string on the status line and calls processActionEvent( ) for
the superclass. Because MyButton is an inner class, it has direct access to the showStatus( )
method of ButtonDemo2.
/*
* <applet code=ButtonDemo2 width=200 height=100>
* </applet>
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ButtonDemo2 extends Applet {
MyButton myButton;
static int i = 0;
public void init() {
myButton = new MyButton("Test Button");
add(myButton);
}
class MyButton extends Button {
public MyButton(String label) {
super(label);
enableEvents(AWTEvent.ACTION_EVENT_MASK);
}
protected void processActionEvent(ActionEvent ae) {
showStatus("action event: " + i++);
super.processActionEvent(ae);
}
}
}
Extending Checkbox
The following program creates an applet that displays three check boxes labeled "Item 1",
"Item 2", and "Item 3". When a check box is selected or deselected, a string containing the
name and state of that check box is displayed on the status line of the applet viewer or
browser.
The program has one top-level class named CheckboxDemo2 that extends Applet. Its init( )
method creates three instances of MyCheckbox and adds these to the applet. MyCheckbox is
an inner class that extends Checkbox. Its constructor uses super to pass the label of the check
box to the superclass constructor. It calls enableEvents( ) so that item events may be received
by this object. When an item event is generated, processItemEvent( ) is called. That method
displays a string on the status line and calls processItemEvent( ) for the superclass.
/*
* <applet code=CheckboxDemo2 width=300 height=100>
* </applet>
*/
import java.awt.*;
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home