Java Reference
In-Depth Information
3. Create an action listener, especially:
(a) Define in method actionPerformed what has to happen in response to an
event.
(b) Use the ActionEvent object to get information about the type of event that
occurred.
4. Assign the action listener to the relevant graphical components of the view.
The user-program interaction then works as follows:
The user presses a button.
The runtime system detects the user action and creates an event object which
contains information about the action.
The runtime system calls method actionPerformed of the listener which is
assigned to the button. The event object is passed to actionPerformed as an
argument.
The code in method actionPerformed is executed.
Exercises
3.1
Add a third button to the CounterPanel .Itshould be located at the bottom
and labelled 'Reset'. When pressing this button, the counter should be reset
to 0. Then write an application that displays the new panel as in Figure 3.5.
3.2
Write an application that displays two counters as shown in Figure 3.6. Try to
'recycle' components already defined, e.g. CounterPanel .
3.3
Write an application that displays four buttons labelled '1', '2', '3' and '4' and
a label. Arrange the components as shown in Figure 3.7. The label initially
displays the text 'No button pushed'. When one of the four buttons is pushed
the text in the label changes to 'Last button pushed was no. X', where X is the
number of the button.
Up
14
Down
Figure 3.5 Layout for the GUI in Exercise 3.1
Search WWH ::




Custom Search