7.9. CHOICE COMPONENTS
89
(action (lambda(e) (.resize w 500 500))))
(button "small" green
(action (lambda(e) (.resize w 10 10))))
(button "just right" yellow
(action (lambda(e) (.pack w))))
(button "gone"
(action (lambda(e) (.hide w))))
)))
(.pack w)
(.show w)
Observe that we add an action to a button by adding a term of the following
form to its argument list:
(action (lambda(e) .....))
where the .... represents some scheme code that will be executed when the
button is pushed.
7.9
Choice components
The choice procedure produces a component that allows the user to choose
from a fixed set of choices. The following GUI lets the user make a choice of
size for the window:
(define w
(window "Hello"
(choice "big" "small" "just right" "gone"
(action (lambda(e)
(case (readstring (.getSource e))
(("big") (.resize win 500 500))
(("small") (.resize win 10 10))
(("just right") (.pack win))
(("gone") (.hide win))
))))))
(.pack w)
(.show w)
This example shows one way of writing an action that depends on the choice
taken by the user. The tricky part is the
(readstring (.getSource e))
The e in this case is the event that occurs when the user presses selects a
choice. The .getSource procedure determines the component that generated
that event (in this case, the choice), and the readstring reads the string la
belling that choice. The rest of the action is a simple case expression that
selects the appropriate action based on the user's choice.