Page 367 - Introduction to Programming with Java: A Problem Solving Approach
P. 367
8.13 GUI Track: Problem Solving with CRC Cards (Optional) 333
This program imports the javax.swing package to provide access to three classes in the Java API: JFrame, JTextArea, and JSplitPane. In a main method, it repeatedly asks the user for another class until a ‘q’ entry says it’s time to quit. After the user enters each class name, the program instantiates a small JFrame window that represents one CRC card. The JFrame constructor automatically inserts the text, “Class: <classname>” in that window’s header and thereby implements the first ‘C’ in CRC. Then the program instantiates two JTextArea “panes,” which act like little erasable scratch pads, on which you can write any text anywhere. The two JTextArea constructor calls automatically write “RESPONSI- BILITIES:” and “COLLABORATORS:,” respectively, on the first line of each of these two JTextArea panes. Then, the program instantiates a JSplitPane with a HORIZONTAL_SPLIT specification that splits the window into two side-by-side “openings” separated by a moveable vertical partition. The last two JSplitPane parameters paste the individual JTextArea panes into these two openings.
The setSize method call sizes the window to make it about like a 3" 5" file card. The add method call adds the split pane to the window. The setLocationByPlatform method call tells the computer to offset each additional card so that you can continue to see the titles and borders of previously created cards as they “pile up” on your desktop. The setVisible method makes each new card visible. The toFront method moves it to the front of your screen. The setDividerLocation method positions the JSplitPane divider two-thirds of the way to the right, to provide twice as much space for “responsibili- ties” text as for “collaboration” text. The specified window dimensions and the location of the split-pane divider are just initial settings, and if you find you need more space, you’ll be able to change them interac- tively on the computer screen at any time while the program is running.
When you run the program in a Windows environment, you’ll get a Command Prompt window with a query asking for a class name, like what appears in Figure 8.14.
Apago PDF Enhancer
Figure 8.14 Initial Command Prompt display for CRCCards program
After you enter a class name, an additional window appears. This is your first CRC card. If the Com- mand Prompt window is now underneath the new card, drag the Command Prompt window down and to the right, to get it out of the way. Then, move the cursor to the new CRC card, and fill in additional information, like the “run program” entry in the RESPONSIBILITIES pane and the “GarageDoorSystem” entry in the COLLABORATORS pane in Figure 8.15.
Go back to the Command Prompt and enter another class name, and so on. until you have created all the CRC cards you need. They should automatically pile up in a “stack” that looks something like the four cards in the upper left of the computer screen shown in Figure 8.16.
Now, before you enter a ‘q’ in the Command Prompt window, you can reduce it to an icon and play around with four CRC cards. You can drag them anywhere on the screen to form logical hierarchies or