Button with Action
Open the Java console to see what happens when you click the button.
This applet illustrates:
- Printing on the Java console with System.out.println
- Adding "life" to a button via implements ActionListener on the
applet, and having a new method called actionPerformed. Notice also
the addActionListener in init, for the new button.
- The use of this, a Java construct used for refering to the current
object, i.e. the one for which you are writing the class. In this example, we
are writing a class called myApplet which extends (i.e. is a type of) Applet. Here,
this is used to let the button know what object is handling the mouse clicks on the button:
it is the current object, our applet when it will be running.
- In actionPerformed, notice the use of ActionEvent: this is what will specify the
type of event, when it will happen. Then, notice the use of getSource() for the event e:
it gets the object that caused the event. In our case, we want to see whether the button was clicked.