35-Interactors

35-Interactors - CS106A Handout 35 May 20th 2011 Spring...

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

View Full Document Right Arrow Icon
CS106A Handout 35 Spring 2011 May 20 th , 2011 Interactors: Components And Listeners We’re going to spend some more time discussing some more advanced GUI design, and in doing so we’ll rewind back to Sections 10.7 and 10.8 in the textbook (incorrectly listed as Section 10.7 in the syllabus—sorry about the typo). The new material is very fun, emblematic of the type of work that quite possibly drew you to take a CS class, and it’ll be instrumental to your work on the final assignment, which has you build a miniature version of Facebook called FacePamphlet. Simple Button GUI Example Here’s a simple example illustrating how a single JButton can be added to the southern strip of a ConsoleProgram and be wired up to populate the center region (always a console in a ConsoleProgram subclass) with static text. public class SimpleButton extends ConsoleProgram { /* Initializes the user-interface buttons */ public void init() { setTitle("Press Me"); setFont("Palatino-BOLD-24"); add( new JButton("Press Me"), SOUTH ); addActionListeners(); } /* Responds to a button action */ public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Press Me")) { println("Press me again!"); } } } The new banner item here is the actionPerformed methods, which is to the JButton what mousePressed , mouseMoved , mouseReleased , et al. are to mouse clicks. The addActionListeners method—already defined for all Program subclasses—is implemented to set all JButton s to listen for action events, and the fire the actionPerformed method when action events are generated. The one example above only has one button, so in practice it’s always clear what button fires the event. But more elaborate applications might have many buttons with a single actionPerformed method handling any and all action events. That means actionPerformed must ascertain which button fired the event to figure out how the program should respond.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Another Simple Button GUI Example [courtesy of Eric Roberts] This larger example illustrates how multiple JButton s can be used to decide whether drivers should stop, go, or yield. There are two classes—the GStoplight and the Stoplight classes—that combined manage the entire program. GStoplight
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/28/2011 for the course CS 106A taught by Professor Sahami,m during the Spring '08 term at Stanford.

Page1 / 7

35-Interactors - CS106A Handout 35 May 20th 2011 Spring...

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

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