An Introduction To Graphical User Interfaces The eventdriven

  • Slides: 19
Download presentation
An Introduction To Graphical User Interfaces The event-driven model Building a simple graphical interface

An Introduction To Graphical User Interfaces The event-driven model Building a simple graphical interface in Java An introduction into HCI: Task-Centered System Design James Tam

Traditional Software Program control is largely determined by the program through a series of

Traditional Software Program control is largely determined by the program through a series of statements. Start If (day = SAT) OR (day = SUN) “Closed for the weekend” else. . . End An introduction to graphical user interfaces in Java James Tam

Traditional Software Program control is largely determined by the program through a series of

Traditional Software Program control is largely determined by the program through a series of statements. Start “Enter password” The program controls execution Input An introduction to graphical user interfaces in Java James Tam

Event-Driven Software Program control can also be determined by events RAM OS Program An

Event-Driven Software Program control can also be determined by events RAM OS Program An introduction to graphical user interfaces in Java Current point of execution James Tam

Event-Driven Software Program control can also be determined by events RAM OS Program Current

Event-Driven Software Program control can also be determined by events RAM OS Program Current point of execution New point of execution (to handle the key press) An introduction to graphical user interfaces in Java James Tam

Characteristics Of Event Driven Software Program control can be determined by events as well

Characteristics Of Event Driven Software Program control can be determined by events as well standard program control statements The typical source of these events is the user These events can occur at any time An introduction to graphical user interfaces in Java James Tam

Onscreen Objects Can Trigger Events Graphical objects can be manipulated by the user to

Onscreen Objects Can Trigger Events Graphical objects can be manipulated by the user to trigger events. Each object can have 0, 1 or many events that can be triggered. An introduction to graphical user interfaces in Java James Tam

Steps In The Event Model 1) The graphical component must register all interested event

Steps In The Event Model 1) The graphical component must register all interested event listeners. 2) The user triggers an event by manipulating that component 3) The component sends a message to all listeners of that event 4) The event listener provides code to handle the event An introduction to graphical user interfaces in Java James Tam

1. The Graphical Component Must Register All Interested Event Listeners. Class My. Listener extends

1. The Graphical Component Must Register All Interested Event Listeners. Class My. Listener extends Action. Listener { () is n. L o i t c dd. A er ten } b. a … Button (component) An introduction to graphical user interfaces in Java James Tam

2. The User Triggers An Event By Manipulating That Component An introduction to graphical

2. The User Triggers An Event By Manipulating That Component An introduction to graphical user interfaces in Java James Tam

3. The Component Sends A Message To All Registered Listeners For That Event class

3. The Component Sends A Message To All Registered Listeners For That Event class My. Listener extends Action. Listener { public void action. Performed (Action. Event e) { } An introduction to graphical user interfaces in Java James Tam

3. The Component Sends A Message To All Registered Listeners For That Event class

3. The Component Sends A Message To All Registered Listeners For That Event class My. Listener extends Action. Listener { public void action. Performed (Action. Event e) { button. set. Text(“Stop pressing me”); } An introduction to graphical user interfaces in Java James Tam

4. The Event Listener Provides Code To Handle The Event class My. Listener extends

4. The Event Listener Provides Code To Handle The Event class My. Listener extends Action. Listener { public void action. Performed (Action. Event e) { button. set. Text(“Stop pressing me”); } An introduction to graphical user interfaces in Java James Tam

Building A Simple GUI This example can be found in the directory: /home/profs/tamj/233/examples/GUIs An

Building A Simple GUI This example can be found in the directory: /home/profs/tamj/233/examples/GUIs An introduction to graphical user interfaces in Java James Tam

The Driver Class import java. awt. *; import java. awt. event. *; public class

The Driver Class import java. awt. *; import java. awt. event. *; public class Driver { public static void main(String[] args) { Frame f = new Frame (); Panel p = new Panel (); f. add(p); Button make. Popup = new Button ("Popup"); Button goes. Invisible = new Button("Press me"); Button quit. Program = new Button ("Quit"); My. Listener simple. Listener = new My. Listener (); An introduction to graphical user interfaces in Java James Tam

The Driver Class (2) height // Para 1 = x coordinate, Para 2 =

The Driver Class (2) height // Para 1 = x coordinate, Para 2 = y coordinate, Para 3 = width, Para 4 = f. set. Bounds(100, 200, 300, 200); p. add(goes. Invisible); p. add(make. Popup); p. add(quit. Program); // Register simple. Listener as a listener to when the buttons get pressed goes. Invisible. add. Action. Listener(simple. Listener); make. Popup. add. Action. Listener(simple. Listener); quit. Program. add. Action. Listener(simple. Listener); // Make the frame visible f. show(); } } An introduction to graphical user interfaces in Java James Tam

Class My. Listener import java. awt. *; import java. awt. event. *; public class

Class My. Listener import java. awt. *; import java. awt. event. *; public class My. Listener implements java. awt. event. Action. Listener { public void action. Performed(Action. Event e) { String s = e. get. Action. Command(); if (s. equals("Popup")) { Frame f = new Frame ("Popup window"); f. set. Bounds(0, 0, 200, 100); f. show(); for (int i = 0; i < 10000; i++); f. set. Title("Going away soon"); for (int i = 0; i < 10000; i++); f. set. Visible(false); } An introduction to graphical user interfaces in Java James Tam

Class My. Listener else if (s. equals("Press me")) { Button b = (Button) e.

Class My. Listener else if (s. equals("Press me")) { Button b = (Button) e. get. Source(); b. set. Visible(false); } else if (s. equals("Quit")) { System. exit(0); } } } An introduction to graphical user interfaces in Java James Tam

Summary You should now know: • The difference between traditional and event driven software

Summary You should now know: • The difference between traditional and event driven software • How event-driven software works • A simple graphical interface example An introduction to graphical user interfaces in Java James Tam