Do you expect that applets have a main
method?
No.
The methods of an applet are called by another program,
not by its own main
method.
An applet is an object that is used by another program,
typically a Web browser.
The Web browser is the application program and (at least conceptually)
holds the main()
method.
The applet part of a Web page
provides services (methods) to the browser when the browser asks for them.
An applet object has many
instance variables and methods.
Most of this is in the definition of Applet
.
To access these definitions,
your program should import java.applet.Applet
and java.awt.*
.
Here is the code for a small applet.
The name of the class is AEHousman
and the name of the source file is AEHousman.java.
import java.applet.Applet; import java.awt.*; public class AEHousman extends Applet { public void paint ( Graphics gr ) { setBackground( Color.pink ); gr.drawString("Loveliest of trees, the cherry now", 25, 30); gr.drawString("Is hung with bloom along the bough,", 25, 50); gr.drawString("And stands about the woodland ride", 25, 70 ); gr.drawString("Wearing white for Eastertide." ,25, 90); gr.drawString("--- A. E. Housman" ,50, 130); } }
Compile the applet just like an application
using javac AEHousman.java
.
This produces a bytecode file called AEHousman.class
.
Now it can be used in a Web page.
Here is what this applet does:
This applet is explained in the next several pages.