Where does
a Java applet start?
For an applet, the main class is the
one referred to in the <applet> tag of the HTML file, e.g.,
<applet
code=MyClass width = 300 height=300>
</applet>
As we shall see later, a constructor
is the method that is executed when an instance of a class is created.
The constructor method always has the same identifier (name) as the class
itself. So the first statement to be executed for the applet MyClass will
be the first statement of the constructor method MyClass.
Example:
The following applet will display a
message on the screen.
//AppletGreetings.java
import java.awt.*;
import java.applet.Applet;
public class
AppletGreetings extends Applet {
public void paint (Graphics g){
g.drawString(" Welcome to CMT4120", 50,50);
}
}
You first compile the AppletGreetings.java
using TextPad:
The HTML file is:
<applet
code="AppletGreetings.class" width=300 height=50>
</applet>
You can then run the HTML file from
TextPad using clicking Tools and selecting Run Java Applet.
Back
to top
RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex
University. All rights reserved.
webmaster@globalcampus.com.eg
|