Lesson EightCreating applets | |
Now it's time to have some fun. Everything that you've learned so far can be put into action by the use of applets. To see what you'll start creating, click here. This applet requires an HTML document to host the applet, labels to produce the text output, codes to produce text fields and buttons, and event-driven programming to make the text fields and buttons operate. First, let's start with the HTML host. Here's what it'll look like: That's it. The applet code reads the "Greet.class" statement and runs the program. To learn more about HTML, click here. Now it's time to write the program. In order to create and applet, you need to import a few classes: Each one of these are packets that help you with your creations. The java.applet.*; provides you with the tools to create the applets. The java.awt.*; is the Abstract Windows Toolkit that contains components such as labels, menus, and buttons so you don't have to create them too. The java.awt.event.*; includes event classes used in event-driven programming. To use these packets, you need to add the phrase extends Applet implements ActionListener. Below is the next part of the program that calls the classes to be used in the program: Next, we'll create the part of the program that greets the user and asks for their name, provides a text box for the user to type their name, and a button to press after their name is typed. To add a component to an applet window, you need to insert the add(); method. The .addActionListener(this); calls for the event-driven programming that will process this applet. Finally, we'll create the event-driven program that will take what the user types in the text box and prints a greeting after they press the button. You can also send much needed information to your applets through what are called parameters. Applets can't query what parameters are needed, so you need to set these in advance. You can support unlimited numbers of parameters in two different ways. We'll take a look at the more simple version. You specify each parameter separately with a common naming convention: These are added between your <APPLET> and </APPLET> tags. If you want to see some more applets at work, go to my applets and view them for yourself. You can also learn more about all of the applet classes and methods at java.sun.com. |
|
Question: These are some cool gadgets. But what if I wanted to add some colors to the text or background? Is there a way I can do that and maybe create my own graphics too? Answer: I thought you'd never ask. The next lesson you'll be learning will be all about graphics. | |
If you have a questions about any of the lessons, feel free to ask. |