Java Reference
In-Depth Information
public
public void
void init () {
JButton b ;
requested = false
false ;
Container cp = ( Container ) getContentPane ();
cp . setLayout ( new
new FlowLayout ());
String buttonLabel = getParameter ( "buttonlabel" );
iif ( buttonLabel == null
null ) {
buttonLabel = "Draw/Don't Draw" ;
}
cp . add ( b = new
new JButton ( buttonLabel ));
b . addActionListener ( new
new ActionListener () {
/* Button - toggle the state of the "requested" flag, to draw or
* not to draw.
*/
public
public void
void actionPerformed ( ActionEvent e ) {
String arg = e . getActionCommand ();
// Invert the state of the draw request.
requested = ! requested ;
do_the_work ();
}
});
}
/** paint() is an AWT Component method, called when the
* component needs to be painted.
*/
public
public void
void do_the_work () {
/* If the Draw button is selected, draw something */
iif ( requested ) {
showStatus ( "Welcome to Java!" );
} else
else {
showStatus ( "" );
// retract welcome? :-)
}
}
}
If we run it in my AppletViewer , [ 64 ] it shows up as a window with just the Draw button
showing; if you click the button an odd number of times, the screen shows the welcome label
( Figure 23-2 ).
 
Search WWH ::




Custom Search