content









Content










10.3
GUI
Functionality




10.3.1
Event
model





An event is an action initiated by a
user.  Some common events are
button pressing (or button clicking), and changing text. Figure

illustrates the concept of an event. When the user performs an action
at the user interface level (clicks a mouse or presses a key), this
causes an event to be issued. Events are objects that describe what
has happened. A variety of event classes exist to describe different
categories of user action.
In Java, event objects are derived from the java.awt.AWTEvent class. The first experience that most programmers have with event driven
programming is in the context of GUIs and managing user interactions.
However, there are many other types of events that can be created to
handle non-GUI related business tasks. Classes that handle events can
be found in the java.awt.event package, as well as in other packages.
Examples of event objects include
ActionEvent, WindowEvent, ComponentEvent, ItemEvent, TextEvent,
KeyEvent, and MouseEvent.
The Java language uses an event
delegation model to handle events. Figure

illustrates the objects that are used in this model.

A source of an event This
is an object that can generate an event object.
The event This is an event
object that encapsulates information about the event.
The handler This is the
object that can handle the event, which is performing some task.

This delegation model is shown in
Figure . The button is the source. The button generates the ActionEvent
object when the user initiates an action. The handler is the class that
implements the specific interface that is appropriate for the event.
The code that the programmer will write
to implement the event driven model consists of the previous three
steps. The event delegation
model can be implemented all in one class. In other words, the class
that contains the GUI components also implements the Listener
interfaces. In general, when implementing a proper Model View
Controller (MVC) pattern, the GUI should be defined in one class, the
business logic in a separate class, and the listener methods in
another class.
The MVC pattern uses three main
components:

Model
View
Controller

The Model represents the logical
structure of data in the application, and does not contain any
information about the user interface.
The View represents elements in the
user interface.
The Controller represents classes which
connect the Model and the View, and are used to communicate between
Model classes and View classes.
In some cases if the handler for the
event is specific to a GUI and its components, the programmer can use
two special types of class definitions, the inner class or the
anonymous class, to handle the events. A later section describes these
classes and their use.
 










Wyszukiwarka

Podobne podstrony:
content
content
content
content
content
content
content
content
content
function domnode get content
content
content
content
content
content
content

więcej podobnych podstron