CH10-EventDrivenPrograms

CH10-EventDrivenPrograms - The Art and Science of An...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Jav a Event-driven Programs C H A P T E R 1 0 I claim not to have controlled events, but confess plainly that events have controlled me. 10.1 The Java event model 10.2 A simple event-driven program 10.3 Responding to mouse events 10.4 Responding to keyboard events 10.5 Creating a simple GUI Abraham Lincoln, letter to Albert Hodges, 1864 10.6 The Swing interactor hierarchy 10.7 Managing component layout 10.8 Using the TableLayout class The Java Event Model Graphical applications usually make it possible for the user to control the action of a program by using an input device such as a mouse. Programs that support this kind of user control are called interactive programs . User actions such as clicking the mouse are called events . Programs that respond to events are said to be event-driven . In modern interactive programs, user input doesnt occur at predictable times. A running program doesnt tell the user when to click the mouse. The user decides when to click the mouse, and the program responds. Because events are not controlled by the program, they are said to be asynchronous . When you write a Java program, you indicate the events to which you wish to respond by designating some object as a listener for that event. When the event occurs, a message is sent to the listener, which triggers the appropriate response. The Role of Event Listeners One way to visualize the role of a listener is to imagine that you have access to one of Fred and George Weasleys Extendable Ears from the Harry Potter series. ListenerExample Suppose that you wanted to use these magical listeners to detect events in the canvas shown at the bottom of the slide. All you need to do is send those ears into the room where, being magical, they can keep you informed on anything that goes on there, making it possible for you to respond. Event Types Java events come in many different types. The event types used in this book include the following: Mouse events , which occur when the user moves or clicks the mouse Keyboard events , which occur when the user types on the keyboard Action events , which occur in response to user-interface actions Each event type is associated with a set of methods that specify how listeners should respond. These methods are defined in a listener interface for each event type. As an example, one of the methods in the mouse listener interface is mouseClicked . As you would expect, Java calls that method when you click the mouse. Listener methods like mouseClicked take a parameter that contains more information about the event. In the case of mouseClicked , the argument is a MouseEvent indicating the location at which the click occurred. A Simple Event-driven Program import acm.program.*; import java.awt.event.*; /** Draws a star whenever the user clicks the mouse */ public class DrawStarMap extends GraphicsProgram {...
View Full Document

This note was uploaded on 02/18/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Page1 / 56

CH10-EventDrivenPrograms - The Art and Science of An...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online