15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Making User Interfaces with Event-Driven Programming 39<br />

Un<strong>for</strong>tunately, although pull-down menus made programs easy <strong>for</strong> users,<br />

they made writing programs much harder <strong>for</strong> the programmers:<br />

Book I<br />

Chapter 2<br />

✦ Programmers had to write extra commands just to display all these<br />

fancy pull-down menus and windows. (Even worse, programmers had to<br />

make sure all those extra commands used to create pull-down menus<br />

and windows actually worked correctly.)<br />

✦ Programmers now had to write programs that could react to whatever<br />

command the user chose. Rather than present the user with options in a<br />

specific, predictable order, programs had to handle the unpredictable<br />

choices of the user.<br />

Different Methods<br />

<strong>for</strong> Writing<br />

Programs<br />

To solve this dual problem of creating pull-down menus and knowing how to<br />

handle the different commands the user might choose at any given time,<br />

computer scientists developed event-driven <strong>programming</strong>.<br />

In event-driven <strong>programming</strong>, an event is something that the user does, like<br />

clicking a pull-down menu or clicking a button displayed in a window. Eventdriven<br />

<strong>programming</strong> simply focuses on displaying different commands onscreen<br />

and then handling these different events when they occur.<br />

Event-driven <strong>programming</strong> divides <strong>programming</strong> into three distinct parts:<br />

✦ The user interface: The commands the user sees on-screen<br />

✦ The event handler: The part of your program that reacts to the commands<br />

the user chooses from the user interface<br />

✦ The actual program: The part of your program that actually does something<br />

useful, such as drawing pictures or predicting the winners of horse<br />

races<br />

In the old days, creating a user interface essentially tripled your work:<br />

1. Write your program.<br />

2. Write commands to create a user interface.<br />

3. Write commands to make your user interface actually work.<br />

Event-driven <strong>programming</strong> eliminates this problem. Instead of <strong>for</strong>cing you to<br />

write commands to display pull-down menus and windows on-screen, eventdriven<br />

<strong>programming</strong> lets you visually design your user interface, such as the<br />

number, placement, and size of buttons.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!