Objectives:
When we
press on a button it produces an event. Different types of widgets may
produce different types of events. Buttons produce action
events. In order for our program to be able to do something when a
button is pressed, we have to direct it to "listen" for action events.
There are three steps involved with this. First we specify that the
class implements an ActionListener in the class definition at the
top. Secondly, we register each button in the constructor to say that
the button's action events will be handled by this listener. Thirdly,
we must include a method called actionPerformed. This method
will be executed every time an action event is heard.
The
actionPerformed method has an ActionEvent e as a parameter. We
can check to see which button produced that event by using e.getSource().
In this case the method is checking to see if the open button or the
play button was pressed and then either creates a new Sound object
from the provided file, or plays the sound.
| Assignment: Add two more buttons to the SoundPlayer: A button which will cause the sound to play with an echo, and a button which will cause the sound to play in reverse. |
| Assignment: Add a slider to the SoundPlayer to control the volume. You will want to use the setVolume method that you have written previously to set the new volume of your sound. |