1 00:00:00,000 --> 00:00:00,270 2 00:00:00,270 --> 00:00:03,170 SPEAKER 1: Let's write a program with a graphical user interface as well as 3 00:00:03,170 --> 00:00:07,240 a text field that allows the user to type in some string and hit Enter, at 4 00:00:07,240 --> 00:00:09,920 which point that string is provided to my program. 5 00:00:09,920 --> 00:00:13,360 Much like GetString in the CS50 Library works, but this time with a 6 00:00:13,360 --> 00:00:14,880 graphical user interface. 7 00:00:14,880 --> 00:00:16,180 Let's get started. 8 00:00:16,180 --> 00:00:19,810 I've already written some skeletal code, so let's now fill in a blank. 9 00:00:19,810 --> 00:00:22,770 GTextField, calling it field. 10 00:00:22,770 --> 00:00:25,790 Assigning it the return value of newGTextField. 11 00:00:25,790 --> 00:00:28,790 And specifying that I'd like to see 10 characters from the user on 12 00:00:28,790 --> 00:00:30,440 the screen at a time. 13 00:00:30,440 --> 00:00:35,720 Let's next call setActionCommand, passing in field, passing in say, 14 00:00:35,720 --> 00:00:39,330 quote unquote "input." In other words, let's associate with this text field 15 00:00:39,330 --> 00:00:43,620 unique string "input." just in case my user interface has multiple text 16 00:00:43,620 --> 00:00:47,260 fields, this string will uniquely identify this one. 17 00:00:47,260 --> 00:00:53,000 Let's next call addToRegion, passing in window, passing in field, passing 18 00:00:53,000 --> 00:00:56,780 in quote unquote "SOUTH," a unique identifier defined in the Stanford 19 00:00:56,780 --> 00:00:59,680 Portable Library that specifies the southern or bottom 20 00:00:59,680 --> 00:01:01,560 region of my user interface. 21 00:01:01,560 --> 00:01:05,230 Let's next induce an infinite loop. 22 00:01:05,230 --> 00:01:07,870 And inside of this loop, let's listen for two events. 23 00:01:07,870 --> 00:01:09,910 One, the user closing the window. 24 00:01:09,910 --> 00:01:13,060 Or two, the user typing something into that text field. 25 00:01:13,060 --> 00:01:15,600 Let's declare a gActionEvent. 26 00:01:15,600 --> 00:01:16,770 Calling it event. 27 00:01:16,770 --> 00:01:19,700 Assigning it the return value of waitForEvent. 28 00:01:19,700 --> 00:01:24,720 Specifying that the type of event we'd like to listen for is an ACTION_EVENT, 29 00:01:24,720 --> 00:01:28,470 where ACTION_EVENT is a constant declared in the Stanford Portable 30 00:01:28,470 --> 00:01:31,180 Library that specifies that type of event. 31 00:01:31,180 --> 00:01:37,600 Let's next check if, getEventType, passing in event, equals equals 32 00:01:37,600 --> 00:01:42,110 WINDOW_CLOSED, another constant declared in the Stanford Portable 33 00:01:42,110 --> 00:01:45,090 Library that indicates that the window has closed. 34 00:01:45,090 --> 00:01:48,450 Then let's simply break out of this infinite loop. 35 00:01:48,450 --> 00:01:51,600 Otherwise, let's now check whether the user has typed something into that 36 00:01:51,600 --> 00:01:52,910 text field. 37 00:01:52,910 --> 00:02:00,950 If, string compare, getActionCommand, passing in event, comma quote unquote 38 00:02:00,950 --> 00:02:03,510 "input" equals equals 0. 39 00:02:03,510 --> 00:02:07,340 In other words, if the unique identifier the text field into which 40 00:02:07,340 --> 00:02:10,680 the user has typed something is equal to that unique identifier that I 41 00:02:10,680 --> 00:02:13,000 specified earlier, let's do the following. 42 00:02:13,000 --> 00:02:18,970 Printf quote unquote "%s was inputted." And let's plug-in for that 43 00:02:18,970 --> 00:02:24,950 %s the return value of getText passing in the field, where getText is another 44 00:02:24,950 --> 00:02:28,350 function, defined in the Stanford Portable Library, that gets the text 45 00:02:28,350 --> 00:02:29,430 from a field. 46 00:02:29,430 --> 00:02:32,210 Let's now save, compile, and run this program. 47 00:02:32,210 --> 00:02:33,730 Make text. 48 00:02:33,730 --> 00:02:35,630 ./text. 49 00:02:35,630 --> 00:02:38,780 There's my user interface, and there's that text field at the bottom. 50 00:02:38,780 --> 00:02:42,310 Let's type something like h-e-l-l-o, Enter. 51 00:02:42,310 --> 00:02:45,280 And there, at the bottom of my console window, indeed we see 52 00:02:45,280 --> 00:02:46,530 that "hello" was inputted. 53 00:02:46,530 --> 00:02:48,790