Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE Software Engineering Fall 1999 Updated by J. Brown

Similar presentations


Presentation on theme: "CSE Software Engineering Fall 1999 Updated by J. Brown"— Presentation transcript:

1 CSE 470 - Software Engineering Fall 1999 Updated by J. Brown
Events in Java CSE Software Engineering Fall 1999 Updated by J. Brown What is the largest Integer that can be represented? What if we want to represent a larger number? What if we want to represent a fraction? Use Scientific Notation, and Significant digits. 9/18/2018 1 1

2 Example 1-- Simple Action Event
import java.awt.*; Import java.awt.event.*; import java.applet.*; public class SimpleEvent extends Applet implements ActionListener { Button button1 = new Button("Press me"); public void init() { this.add(button1); button1.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource() == button1) { button1.setLabel("Again"); 9/18/2018

3 Handling Events Events are first-class objects.
There is a class Event. Subclasses identify different kinds of events. Components fire events. Other objects can listen for/act upon these events. Interested objects register themselves as a Listener with the component that fires the event. 9/18/2018

4 Event Routing Lots of different kinds of events
Different events carry different types of information E.g., ActionEvent carries a command, TextEvent carries a string being edited. Components fire these events. E.g., Button fires an ActionEvent. E.g., TextArea fires a TextEvent. All components fire WindowEvents. When fired, events are routed to special Listener objects. 9/18/2018

5 The AWTEvent Hierarchy
ActionEvent ComponentEvent TextEvent InputEvent WindowEvent MouseEvent KeyEvent 9/18/2018

6 Listeners A listener is an object to which AWTEvents can be routed by components. Different types of listeners. One for each sub-class of AWTEvent. E.g., ActionListener, ComponentListener, WindowListener. A listener class is abstract: Operations only; no attributes or methods. Declared as an interface in Java. 9/18/2018

7 Example: An ActionEvent Listener
import java.awt.*; import java.applet.*; import java.awt.event.*; class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println(“Button pressed!!”); } public class ActionExample extends Applet { public void init() { Button button = new Button(“Push me”); button.addActionListener(new ButtonListener()); button.addMouseListener(new ButtonMouseListener()); add(button); 9/18/2018

8 Example: A MouseEvent Listener
class ButtonMouseListener implements MouseListener { public void mouseEntered(MouseEvent e) { System.out.println(“Mouse entered button!”); }; public void mouseExited(MouseEvent e) { System.out.println(“Mouse exited button!”); public void mousePressed(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} } 9/18/2018

9 Mouse Events mouseClicked(MouseEvent evt);
Called when the mouse button is clicked mousePressed(MouseEvent evt); Called when the mouse button is depressed mouseReleased(MouseEvent evt); Called when the mouse button is released 9/18/2018

10 More Mouse Events mouseEntered(MouseEvent evt);
Called when the mouse enters the component mouseExited(MouseEvent evt); Called when the mouse leaves the component mouseMoved(MouseEvent evt); Called when the mouse moves with no mouse button depressed mouseDragged(MouseEvent evt); Called when the mouse moves with the mouse button depressed 9/18/2018

11 References G. Cornell and C. Horstmann, “Core Java”, The Sunsoft Press Java Series, Second Edition, 1997 D. Joshi, L. Lemay, and C. Perkins, “Teach yourself Java in Café in 21 days”, Sams.net publishing, 1996 The Java Tutorial D. Geary and A. McClellan, “Graphic Java”, The Sunsoft Press Java Series, 1997 9/18/2018


Download ppt "CSE Software Engineering Fall 1999 Updated by J. Brown"

Similar presentations


Ads by Google