Java Applets which make use of Java 1.1 classes may have difficulty being viewed by Netscape browsers or older versions of Internet Explorer. One may need to use the latest version of appletviewer to test graphical applets using Java 1.1. All browsers should be able to view accurately applets written with Java 1.0.
<html>
<title>Java sample</title>
<body>
Here we have a call to a Java file.
<applet code="Testjava3.class" width=500 height=300>
</applet>
</body>
</html>
In the line
<applet code="Testjava3.class" width=500 height=300>
the code reference refers to the translated code file and gives
its correct URL. The width and height references indicate
the size of the applet window on the web page in pixels.
The actual "writing" of components to the web page is done by the method paint which is automatically invoked in its inherited, default form. This method will "paint" to the page any box or textfield specified by an init method, along with other text or figures specified in a user-defined form (e.g., specified by the drawString method), along with figures specified in other user-defined methods in the applet.
Note that this applet contains several import statements which are usually not necessary in a self-standing Java program.
Since this class inherits all the properties of the built-in Java class of Applet, the code must indictate this by the extends Applet clause.
The identifier t is declared as a Java type TestField which puts a text box on a web page and initializes the contents to its argument. The argument can include both text or an integer or both. The text is printed, and the integer indicates the width of the box in numbers of characters. Since the following code attempts to print out numbers, the numbers must first be converted into character strings so that the invocation of a new copy of t actually displays the number as a character rather than interpreting it as the size of the text field.
This must be done by first casting the int value of i into a class of type Integer which can then make use of a standard method (member function), toString() to convert the integer value into a character string. This becomes the initialization argument of TextField at the instantiation of a new copy of this class which is assigned to t.
After t receives its value, the value is added to the visible applet (i.e., on the web page), via the function add(t).
import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
public class Testjava3
extends Applet
{
TextField t;
public void init()
{
for(int i=0;i<=10;i++)
{
t = new TextField((new Integer(i)).toString());
add(t);
}
}
}
This applet may be seen by clicking on the link to
file java3.html.
This page is maintained by Dennis C. Smolarski, S.J.
dsmolarski@math.scu.edu
© Copyright 1998, Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 5 December 1998.