| Hello World Applet in Java David Matuszek, Villanova University |
Here is everybody's favorite program in Java, this time as an applet:
import java.applet.*;
import java.awt.*;
public class HiWorld extends Applet
{
public void paint (Graphics g)
{
g.drawString ("Hi, world!", 50, 100);
}
}
|
This time, however, you need the following additional file:
<html> <head> <title> Hi World Applet </title> </head> <body> <applet code="HiWorld.class" width=300 height=200></applet> </body> </html> |
To run this program,
HiWorld.java. Do
not change the name of this file.HiWorld.html is the name used in this example,
but it really doesn't matter. javac HiWorld.javaHiWorld.class appletviewer HiWorld.htmlHiWorld.html in your favorite browser. (Make sure Java
is enabled in your browser.)Notes
HiWorld, then it must
be in a file named HiWorld.java. If your operating system has case-sensitive
file names, the first part of the file name (HiWorld in this case) must be
capitalized the same way as in your program. javac requires the name of the file (as you would
expect), that is, HiWorld.java. appletviewer nor your browser cares what the name of your HTML file
is; it only cares that the name specified after code= is correct.