More Blanks to Fill
Now we need to add some logic so that
a different action is performed for each button.
An ActionEvent object will have the command
inside of it.
To get the command, use the event's getActionCommand() method.
For example, if evt refers to an ActionEvent object,
then evt.getActionCommand() is the command.
Since the command is a String, you can use its equals( String )
method to compare it to another string.
import java.awt.*;
import java.awt.event.*;
public class TwoButtons extends Frame implements ActionListener
{
Button redButton = new Button("Red");
Button grnButton = new Button("Green");
TwoButtons() // constructor for TwoButtons
{
setLayout( new FlowLayout() ); // choose the layout manager
redButton.addActionListener( this ); // register the buttonDemo object
grnButton.addActionListener( this ); // with both Buttons.
redButton.setActionCommand( "red" );
grnButton.setActionCommand( "green" );
add( redButton );
add( grnButton );
}
public void actionPerformed( ActionEvent evt)
{
if ( evt.getActionCommand().equals( ________ ) )
setBackground( ____________ );
else
setBackground( Color.green );
repaint(); // ask the system to paint the screen.
}
. . . . . . . .
}
|
|