CSci 101: Introduction to Programming

Lab 6

April 2002
Carlson Hall, room 120


The purpose of this lab is to run the applications of Chapter 12, Window Interfaces Using Swing Objects, and understand a little about them. You'll look at the code for each of them briefly, run the application, then answer a couple of basic questions about each.

The source files are available, and they've all been compiled. You can see them in your web browser at http://babbage.clarku.edu/~cs101/Resources/Savitch/ch12/. The easiest way to run them is to get a console window and set your default directory (i.e. folder) to where they're all stored. First, click on the "Command Line" icon on your desktop to get a "Konsole." After the $ prompt enter

cd ../cs101/public_html/Resources/Savitch/ch12
(The cd command changes your default directory, the directory where you're currently located. The command "cd .." changes your default directory up one level to "/home" where all the user directories are stored. The rest of the command line above goes to the cs101 directory, then its subdirectory public_html, and so forth.) You can now enter
ls
to get a listing of all the files in the directory. Another useful command is "more" as in
more FirstSwingDemo.java
which prints at the console the contents of the file, and you'll see it's the same as what's in your text.

Answer the questions on another sheet of paper, and hand it in when you're finished.

The first program: FirstSwingDemo.java. Page 763, see also page 766 for WindowDestroyer.java. Read the paragraph "A simple window" at the bottom of page 762, and glance through the source code. Now run the program by entering at the $ prompt

java FirstSwingDemo
You should get at grey window with some words in it.

1. What instructions in the program put the words there? (There are two, just copy them down.)

2. How do you close the window and end the program? What do you see in the code that might relate to doing that?

(You can also abort the program by typing CTRL-c in the console window.)

FirstWindowDemo.java. Page 774, see also page 773. Run the program FirstWindowDemo. Look through the source code. You can see in the code that two windows are created.

3. Where is the second window?

4. Can you resize windows?

5. What happens when you close one window?

SecondWindowDemo.java. Page 780, see also page 777. Glance through the source code. Run the program. Note that there are two windows. One is pink, the other blue.

6. For the blue window, what was the line that set it to blue?

7. For the pink window, explain how it got to be set to pink. (Some of the color constants are mentioned on page 778. There are other color constants, and there are ways to specify any possible color.)

BorderLayoutDemo.java. Page 784. Run the program. Note that there are three labels in the window. Resize the window for fun. BorderLayouts have five different panels as shown on page 785.

8. Why is the label "Second label there" at the bottom of the window instead of in the middle? After all, it was the second label placed in the window.

ButtonDemo.java. Pages 793-794. Look at the program, then run it.

9. What color does the window at first? After you press the "Red" button? After you press the "Green" button?

10. Where is the instruction that inserted a "Green" button into the window?

11. Where is the instruction that changes the window to green when you press the "Green" button? How does execution ever reach that instruction?

12. When you press the "Green" button, the actionPerformed method is executed. Where's the instruction that enabled actionPerformed to be executed when the "Green" button is pressed? (You could remove a line from the code, and the button would be disabled. Which one?)

PanelDemo.java. Pages 804-805. After looking briefly at the code, run the program. Note that most of the window is blue, but the bottom portion of the window (called the buttonPanel in the program) is white with a red and a green button near the center.

13. Where is the instruction that inserts the buttonPanel into the window (aclled the contentPane in the program)? Why is the buttonPanel placed at the bottom of the window instead of at the top?

14. Where are the instructions that insert the two buttons into the buttonPanel?

MemoSaver.java. Page 813-814. This application illustrates text areas. Run the program. Do this to see what the program does: Type something in the text area, then press "Save Memo 1." Press "Clear," type something in and press "Save Memo 2." Press "Get Memo 1," then press "Get Memo 2." So you see that both the user and the program can insert text in the text area.

15. What is the method that the program uses to read text that the user entered in the text area?

16. What is the method that the program uses to insert text into the text area sot that the user can read it? (As mentioned on page 816, a Text Field is the same as a Text Area, but a Text Field has only one line.)