Example -- Applet with two buttons

In this example, the applet has two buttons, one which increments the count and keeps track of how many times that button is clicked. The second button resets the count to zero.

Java Runtime Environment is required to properly see the Applet. Please check the following link

Listing 2.1.2 shows the source code for the above program.

The program has two buttons,  clickButton and resetButton. The applet implements the ActionListener for both these buttons. As a result, the method for handling the button click event should be able to distinguish which button click needs to be handled. The argument passed to the actionPerformed method carries the details about the ActionEvent generated by the button click. The method getSource on any event object identifies the component which generated the event. A conditional statement is used to distinguish between the two likely candidates.

The corresponding code in the event handler is as shown below:

       if (e.getSource() == clickButton) {
		count++;	// Increment the count
	}
	else {
		count = 0;
	}