Академический Документы
Профессиональный Документы
Культура Документы
in Java
Events
An event is an action initiated by the user
interacting with the program.
Examples
Keyboard events - pressing a key, holding a key, releasing a key
Mouse events - moving the mouse, clicking the mouse
GUI events - clicking on a button, resizing a window, closing a
window, opening a window
Paint Event
repaint() method - requests an erase and redraw (update) after a small time
delay. When you invoke repaint(), it sends a message to the native GUI to
paint. The GUI then repaints some components on it own.
update() method - erases the component (e.g. by drawing the component in
the background colour, which makes it disappear) . Then it calls the paint()
method.
paint() method - does the drawing, using the Graphics object passed to
it.
What triggers a repaint of a GUI container?
adding or deleting a component, making a component visible or invisible, changing
the size or location of a component
calling validate(). This redoes the layout if necessary deciding on new sizes
and locations of all the components in the container. Most often it gets called
directly by application programmers, after a frame or other container is been
composed but before it has been made visible
setVisible(true) for containers which will typically call validate()
4
Event Hierarchy
Events are organized into hierarchy of event
classes
Event classes contain data relevant to a
particular event type
An event is an object of one of the event
classes
java.util.EventObject
java.awt.AWTEvent
ActionEvent
ContainerEvent
AdjustmentEvent
FocusEvent
KeyEvent
ComponentEvent
InputEvent
ItemEvent
PaintEvent
TextEvent
WindowEvent
MouseEvent
8
Event Sources
The type of an event depends on its source
Example of event sources:
the keyboard
the mouse
the GUI components buttons, text fields, windows
ActionEvent
The ActionEvent class contains specific
information about the event that occurred
Two ActionEvent methods are used to obtain
the source of the event
getActionCommand()
method
of
class
ActionEvent is used to obtain a string containing
the name (e.g. the label on a button, text in a text field)
of the event source
getSource() method returns a reference to the
source of the event (e.g. the GUI component that
generated the event)
10
13
The source maintains a list containing all the listener objects that have registered
to be notified of events of that type
The transmission of an event from an event source to an event listener involves
invoking a method on the listener object by which the source notifies the listener
of the occurrence of an event of a specific type
Example: method actionPerformed (ActionEvent e)
An EventListener interface declares one or more methods which must be
defined in the listener class, and which are invoked by the event source in response to
each specific event type handled by the interface
Example:
EventListener method actionPerformed (ActionEvent e)
14
EventObject
Generate an
Event
Source Object
Register a listener object
Notify
Listener
Listener Event
Event Handler
15
ActionEvent
Generate an
Event
Activate an
event
The button b
b.addActionListener(this)
Notify
Listener
ActionListener
actionPerformed(ActionEvent e)
16
add( componentName );
Add an action listener for the component
componentName.
addActionListener( this );
Write the code to handle the event
17
DEMO
18
19
Listener
21
event listener 1
event listener 2
event listener 3
22
Event Listeners
A listener object can be registered on a source
object to be notified of the occurrence of all events
of the specific class for which the listener object is
designed
The occurrence of an event defined by the
specified class will automatically invoke the
matching method in the listener object
The code in the body of the method is designed by
the programmer to perform the desired action
when the event occurs
23
24
Listener Interfaces
We can create a listener object by writing a
class that implements a particular listener
interface
The Java standard class library contains
several interfaces that correspond to
particular event categories
After creating the listener, we add the
listener to the component that might
generate the event to set up a relationship
between the component, generating the
event and the event listener
25
26
In the declaration for the event handler class, we specify that the class
either implements a listener interface or extends a class that implements
a listener interface
2.
someComponent.addActionListener(instanceOfMyClass);
3.
Example
How do buttons handle mouse clicks?
To detect when the user clicks a button, a program must have
an object that implements the ActionListener interface.
The program must register this object as an action listener on
the button (the event source), using the
addActionListener method.
When the user clicks the button, it generates an action event
and the button's action listeners are notified. This results in
the call of the action listener's actionPerformed method.
The single argument to the method is an ActionEvent object that
gives information about the event and its source
29
Handling Events
A listener object must implement the corresponding listener
interface. A listener for a Button source object, for example, must
implement the ActionListener interface. The
ActionListener interface contains the
actionPerformed(ActionEvent e) method. This
method must be implemented in the listener class. Upon
receiving the notification, the method is executed to handle the
event
An event object is passed to the handling method. The event
object contains information relevant to the event type. In the
ActionEvent you can use e.getsource() method to
obtain the source object to determine whether it is a button or a
text box, or a check box.
30
DEMO
31
add( thirdCheckbox );
thirdCheckbox.addItemListener( this );
fourthCheckbox = new Checkbox( "fourth", c, false );
add( fourthCheckbox );
fourthCheckbox.addItemListener( this );
fifthCheckbox = new Checkbox( "fifth", c, false);
add( fifthCheckbox );
fifthCheckbox.addItemListener( this );
} // end of init
public void itemStateChanged( ItemEvent e)
33
DEMO
34
35
switch (choiceIndex) {
case 0 : backColor = Color.white; break;
case 1 : backColor = Color.green; break;
case 2 : backColor = Color.red; break;
default : backColor = Color.white;
}
setBackground( backColor );
}
public void itemStateChanged( ItemEvent e ) {
if ( e.getSource() == chooseColor ) {
choiceIndex = chooseColor.getSelectedIndex();
repaint();
}
}
36
DEMO
37
38
if ( e.getSource() == shapeList ) {
listIndex = shapeList.getSelectedIndex();
String message = "";
if ( listIndex == 1 )
message += "n";
message += " " + shapeList.getItem( listIndex );
showStatus( "You have choosen to draw a" + message );
repaint();
}
}
}
39
switch (shapeNumber) {
case 0:
g.drawLine(xBegin, yBegin, xEnd, yEnd); break;
case 1:
g.drawOval(xBegin, yBegin, width, higth); break;
case 2:
g.drawRect(xBegin, yBegin, width, higth); break;
}
}
}
40