>> SPEAKER 1: Let's write a program with a graphical user interface as well as
a text field that allows the user to type in some string and hit Enter, at
which point that string is provided to my program.
Much like GetString in the CS50 Library works, but this time with a
graphical user interface.
Let's get started.
>> I've already written some skeletal code, so let's now fill in a blank.
GTextField, calling it field.
Assigning it the return value of newGTextField.
And specifying that I'd like to see 10 characters from the user on
the screen at a time.
>> Let's next call setActionCommand, passing in field, passing in say,
quote unquote "input." In other words, let's associate with this text field
unique string "input." just in case my user interface has multiple text
fields, this string will uniquely identify this one.
>> Let's next call addToRegion, passing in window, passing in field, passing
in quote unquote "SOUTH," a unique identifier defined in the Stanford
Portable Library that specifies the southern or bottom
region of my user interface.
>> Let's next induce an infinite loop.
And inside of this loop, let's listen for two events.
One, the user closing the window.
Or two, the user typing something into that text field.
Let's declare a gActionEvent.
Calling it event.
Assigning it the return value of waitForEvent.
Specifying that the type of event we'd like to listen for is an ACTION_EVENT,
where ACTION_EVENT is a constant declared in the Stanford Portable
Library that specifies that type of event.
>> Let's next check if, getEventType, passing in event, equals equals
WINDOW_CLOSED, another constant declared in the Stanford Portable
Library that indicates that the window has closed.
Then let's simply break out of this infinite loop.
>> Otherwise, let's now check whether the user has typed something into that
text field.
If, string compare, getActionCommand, passing in event, comma quote unquote
"input" equals equals 0.
In other words, if the unique identifier the text field into which
the user has typed something is equal to that unique identifier that I
specified earlier, let's do the following.
>> Printf quote unquote "%s was inputted." And let's plug-in for that
%s the return value of getText passing in the field, where getText is another
function, defined in the Stanford Portable Library, that gets the text
from a field.
>> Let's now save, compile, and run this program.
Make text.
./text.
There's my user interface, and there's that text field at the bottom.
Let's type something like h-e-l-l-o, Enter.
And there, at the bottom of my console window, indeed we see
that "hello" was inputted.