Transcript event

EVENTS
CSC 171 FALL 2004
LECTURE 16
“Traditional” Input
In console applications, user input is under
control of the program
The program asks the user for input in a
specific order
Event based input
The user is in control of the sequencing
Mouse
Keyboard
pull down menus
click buttons
etc.
IN ANY ORDER!
Java event handling

The Java window toolkit has a sophisticated
mechanism that allows a program to specify
the event
 Whenever the user of a graphical program
types characters or uses the mouse the Java
window managers sends a notification to
the program that an event has occurred
Events
User interface events include key presses,
mouse moves, button presses, etc.
As the programmer, you supply event listeners
(objects) for the events you care about.
You need to implement methods for handling
the events in the event listener you supply
You construct listener objects.
Event Listeners

Event Listeners are objects of a class
 You construct listener objects and attach
them to the event sources
– “registration”

Event sources are things like text boxes,
buttons, etc
 Sources of events keep track of the type of
things they listen for
Event Notification
Event notifications happen in event listener
classes.
An event listener class implements an event
listener interface
When an event occurs the event source will
call the methods you supplied in the listener
The invocation of the method will be supplied
with information about the event in an event
class object
The listener is an object
Sometimes an object can be two things
an applet
a listener
Sometimes, we make an entirely different
class
MouseListener
public interface MouseListener {
void mousePressed(MouseEvent event);
void mouseReleased(MouseEvent event);
void mouseClicked(MouseEvent event);
void mouseEntered(MouseEvent event);
void mouseExited(MouseEvent event);
}
MouseSpy
MouseEvent
MouseSpy
MouseListener
mousePressed
MouseEvent
MouseEvent
mouseEntered
mouseReleased
MouseEvent
mouseExited
MouseEvent
mouseClicked
public class MouseSpy implements MouseListener {
public void mousePressed(MouseEvent event){
System.out.println("Mouse pressed . x = "
+ event.getX() + " y = " +
event.getY());
}
public void mouseReleased(MouseEvent event){
System.out.println("Mouse released . x = "
+ event.getX() + " y = " +
event.getY());
}
public void mouseClicked(MouseEvent event){
System.out.println("Mouse clicked . x = "
+ event.getX() + " y = " +
event.getY());
}
public void mouseEntered(MouseEvent event){
System.out.println("Mouse entered . x = "
+ event.getX() + " y = " +
event.getY());
}
public void mouseExited(MouseEvent event){
System.out.println("Mouse exited . x = "
+ event.getX() + " y = " +
event.getY());
}
}
public class Lec14 extends Applet {
public Lec14() {
MouseSpy listener = new MouseSpy();
addMouseListener(listener);
}
}
Components

Java supports things like buttons and fields
 The java awt can vary from machine to
machine
 Swing is more platform independent
Event Registration
textfield1
handler
JTextField object
TextFieldHandler object
public void actionPerformed (
ActionEvent event) {
// deal with it
}
listenerList
textField.addActionListener(handler);
…
Example Applet
<html>
HOW DOES IT WORK?
<head>
<title> Ted Pawlicki's CSC 171 Applet
</title>
</head>
<body>
<applet code=“Lec14b.class" width = 512 height = 512 >
</html>
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
WHAT IS THIS THING?
public class Lec14b extends Applet
implements ActionListener {
private Label prompt1,
prompt2, prompt3, greeting;
private TextField firstname, lastname;
public Lec14b() {
setLayout(null);
prompt1 = new Label("Welcome to Ted Pawlicki's CSC 171
Lec 14b Applet");
prompt2 = new Label("Please enter your first name");
prompt3 = new Label("Please enter your last name");
greeting = new Label();
firstname = new TextField();
lastname = new TextField();
add(prompt1);
prompt1.setBounds(150,50,350,32);
add(prompt2);
prompt2.setBounds(150,100,150,32);
add(firstname);
firstname.setBounds(150,150,150,32);
add(prompt3);
prompt3.setBounds(150,200,150,32);
add(lastname);
lastname.setBounds(150,250,150,32);
firstname.addActionListener(this);
lastname.addActionListener(this);
}
WHAT IS GOING ON?
public void actionPerformed (ActionEvent event){
greeting.setText("Nice to meet you, "
+ firstname.getText() + " "
+ lastname.getText());
add(greeting);
greeting.setBounds(150,300,350,32);
doLayout();
}
WHAT IS THE EVENT?
public void actionPerformed (ActionEvent event){
greeting.setText("Nice to meet you, "
+ firstname.getText() + " "
+ lastname.getText());
System.out.println("Event : " + event);//
add(greeting);
greeting.setBounds(150,300,350,32);
doLayout();
}
Inner Classes
Inner Classes
public JButton makeButton(String label, final int dx,final int dy) {
JButton button = new JButton(label);
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
box.translate(dx, dy);
repaint();
}
};
ButtonListener listener = new ButtonListener();
button.addActionListener(listener);
return button;
}
panel.add(makeButton("Left",-BOX_WIDTH,0));
panel.add(makeButton("Right",BOX_WIDTH,0));
panel.add(makeButton("Up",0,-BOX_HEIGHT));
panel.add(makeButton("Down",0,BOX_HEIGHT));