Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 EVENTS CSC 171 FALL 2004 LECTURE 16

2 “Traditional” Input In console applications, user input is under control of the program The program asks the user for input in a specific order

3 Event based input The user is in control of the sequencing Mouse Keyboard pull down menus click buttons etc. IN ANY ORDER!

4 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

5 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.

6 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

7 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

8 The listener is an object Sometimes an object can be two things an applet a listener Sometimes, we make an entirely different class

9 MouseListener public interface MouseListener { void mousePressed(MouseEvent event); void mouseReleased(MouseEvent event); void mouseClicked(MouseEvent event); void mouseEntered(MouseEvent event); void mouseExited(MouseEvent event); }

10 MouseSpy MouseSpyMouseListener mousePressed MouseEvent mouseReleased MouseEvent mouseClicked MouseEvent mouseEntered mouseExited MouseEvent

11 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()); }

12 public void mouseClicked(MouseEvent event){ System.out.println("Mouse clicked. x = " + event.getX() + " y = " + event.getY()); }

13 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()); }

14 public class Lec14 extends Applet { public Lec14() { MouseSpy listener = new MouseSpy(); addMouseListener(listener); }

15 Components Java supports things like buttons and fields The java awt can vary from machine to machine Swing is more platform independent

16 Event Registration JTextField object textField.addActionListener(handler); TextFieldHandler object public void actionPerformed ( ActionEvent event) { // deal with it } listenerListhandlertextfield1 …

17 Example Applet Ted Pawlicki's CSC 171 Applet HOW DOES IT WORK?

18 import java.awt.*; import java.applet.*; import java.awt.event.*; public class Lec14b extends Applet implements ActionListener { private Label prompt1, prompt2, prompt3, greeting; private TextField firstname, lastname; WHAT IS THIS THING?

19 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();

20 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);

21 firstname.addActionListener(this); lastname.addActionListener(this); }

22 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 GOING ON?

23 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(); } WHAT IS THE EVENT?

24 Inner Classes

25 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; }

26 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));


Download ppt "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."

Similar presentations


Ads by Google