1 / 11

Events in Java

Events in Java. CSE 470 - Software Engineering Fall 1999 Updated by J. Brown. Example 1-- Simple Action Event. import java.awt.*; Import java.awt.event.*; import java.applet.*; public class SimpleEvent extends Applet implements ActionListener {

Ava
Télécharger la présentation

Events in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Events in Java CSE 470 - Software Engineering Fall 1999 Updated by J. Brown

  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"); } } }

  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.

  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.

  5. AWTEvent ActionEvent ComponentEvent TextEvent InputEvent WindowEvent MouseEvent KeyEvent The AWTEvent Hierarchy

  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.

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

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

  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

  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 • http://java.sun.com/docs/books/tutorial/index.htm • D. Geary and A. McClellan, “Graphic Java”, The Sunsoft Press Java Series, 1997

More Related