Java Reference
In-Depth Information
Part B: Creating the Applet Source Code
1. Click New on the File menu to open a new document in TextPad.
2. Type a block comment including your name, the current date, the file name
DavidApplet.java, and a brief description of the purpose of the program.
3. Import the java.awt.*, java.applet.*, and java.awt.event.* packages.
4. Include a header for the DavidApplet class, which extends Applet and
implements the ActionListener.
5. Create components for the applet, including Label, TextField, Button, and
Choice components, as shown in Figure 5-52 on the previous page.
6. Create an init() method. Set a background color of your choice. Use the
GridLayout method to create six rows and four columns. Add the components
you created in Step 5 in order from left to right and then top to bottom as
shown in Figure 5-52. Set the focus to the first TextField component. Use add()
methods to populate the list of items for the two Choice components as follows:
Camping Vehicles: tent, pop-up, travel trailer, fifth-wheel, motor home
Hookups: water only, water and electricity, full hookups, no hookups
7. Use the addActionListener() method to make the buttons clickable.
8. Create an actionPerformed() method that tests for which button was clicked,
using the code shown in Figure 5-53.
32 public void actionPerformed ( ActionEvent e )
33 {
34
String arg = e.getActionCommand () ;
35
if ( arg == "Submit" )
36
{
37
38
}
39
if ( arg == "Clear" )
40
{
41
42
}
43 }
FIGURE 5-53
9. Type the closing brace to close the DavidApplet class. The buttons do not
have to work.
10. Save the file on the Data Disk, using DavidApplet.java as the file name.
Compile and execute the applet.
11. Your component placement may not match the figure exactly. You may
experiment with field widths and the Width and Height of the applet itself
in the HTML file.
12. As an extra credit assignment, write the code to make the Clear button work.
Use the setText() and setSelectedItem() methods to clear each component.
Return the focus to the first TextField component.
Search WWH ::




Custom Search