Creating applets – Java Tutorial

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:

<applet code = "Greet.class" width = 450 height = 200>

That’s it. The applet code reads the “Greet.class” statement and runs the program. Now it’s time to write the program. In order to create and applet, you need to import a few classes:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

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:

public class Greet extends Applet implements ActionListener
Label greeting = new Label("Hello. Who are you?");
Font bigFont = new Font("TimesRoman", Font.ITALIC, 24);
Button pressMe = new Button("Press Me");
TextField answer = new TextField("",10);

Creating Applets

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.

public void init()

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.

public void actionPerformed(ActionEvent thisEvent)
String name = answer.getText();
Label personalGreeting = new Label("");
personalGreeting.setText("Hi, " + name);

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:

<param name="Spanish1" value="Uno">
<param name="Spanish2" value="Dos">
<param name="Spanish3" value="Tres">
<param name="Spanish4" value="Cuatro">
<param name="Spanish5" value="Cinco">
<param name="Spanish6" value="Seis">
<param name="Spanish7" value="Siete">
<param name="Spanish8" value="Ocho">
<param name="Spanish9" value="Nueve">
<param name="Spanish10" value="Diez">
<param name="English1" value="One">
<param name="English2" value="Two">
<param name="English3" value="Three">
<param name="English4" value="Four">
<param name="English5" value="Five">
<param name="English6" value="Six">
<param name="English7" value="Seven">
<param name="English8" value="Eight">
<param name="English9" value="Nine">
<param name="English10" value="Ten">

These are added between your <APPLET> and </APPLET> tags.

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.