Below is an example of a simple (and pretty much useless) application, apply named SimpleApplication, that prints a single line of text. A hallmark of applications is that they always have a main method.
class SimpleApplication {
public static void main(String[] args) {
// Print the line of text
System.out.println("I am a very boring application.");
}
}
This results in the following application:
Below is an example of a very simple applet, unsurprisingly named SimpleApplet. It too prints a single line of text, this time to the applet pane in a web browser. Applets do not have a main method and they need to import either the java.applet.Applet or the javax.swing.JApplet class. (The later is needed if you use swing components.)
import java.applet.Applet;
import java.awt.Graphics;
public class SimpleApplet extends Applet {
public void init() {
resize(150,25);
}
public void paint(Graphics g) {
g.drawString("I am a very boring applet", 50, 25);
}
}
The applet is then included in a web page using the object tag, for this applet. For instance:
<applet code="SimpleApplet.class" width="150" height="40">
Simple Java Applet
</applet>
You may also see the applet tag, however, be aware that it is depreciated. The equivalent applet tag would be:
<object codetype="application/java"
classid="java:SimpleApplet.class"
width="150" height="40" >
Simple Java Applet
</object>
You can see this applet in action and download the source code for both the applet and the application here.
Need ready to use applets for your web pages? Instant Java is your answer. Order from Amazon.com or Alibris. |