Java Reference
In-Depth Information
Using the concepts you learned in Chapter 8, perform the following steps:
1. Start TextPad. Enter the program comments, then import the java.io.*,
java.awt.*, java.awt.event.*, javax.swing.*, java.text.*, and java.util.*
packages.
2. Create a class header named Transfer, which extends JFrame and implements
ActionListener.
3. Save the file on your Data Disk using the file name Transfer.java.
4. Type DataOutputStream output; to declare an output stream connection
to the data file.
5. Construct two JPanels, four JLabels, four JTextFields, and two JButtons. Give
them user-friendly names.
6. Create a main method that instantiates a Transfer window and then sets its
title, size, and visibility.
7. Create a constructor method named Transfer, which sets a content pane to a
BorderLayout, sets the first panel to a 4 3 2 GridLayout, and sets the second
panel to a FlowLayout.
8. Within the Transfer() method, add the labels to the first panel and the but-
tons to the second panel. Add the panels to the BorderLayout and add the
ActionListener to each button.
9. Type the code, output = new DataOutputStream(new
FileOutputStream("Transfer.dat")); inside a try block.
10. Type the code, System.exit(1) ; inside the catch(IOException ex) block.
11. Lines 156 through 166 in Figure 8-21 on page 509 display the
addWindowListener() method. Enter those lines to construct the window
listener at the end of the Transfer() constructor method.
12. Create an actionPerformed() method. Within the block, type String arg =
e.getActionCommand(); to declare the button's argument, which is passed
to the actionPerformed event when the user clicks a button.
13. Type if (arg == "Submit") and then press the ENTER key. Type an open-
ing brace to begin the if block.
14. Create a second try block that contains output.writeUTF() methods to send
each of the four JTextFields to the data file. For example, if your JTextField is
called student, the code output.writeUTF(student.getText()); would
send that data to the data file. After the four statements, close the try block.
15. Create a catch(IOException ex) block that calls the System.exit() method.
16. Call a method named clearFields() and then close the if block.
17. Create an else block by typing the code from Table 8-14 on the next page.
(continued)
Search WWH ::




Custom Search