Applet Java Program :-

// A Hello World Applet 
// Save file as 

import java.applet.Applet; 
import java.awt.Graphics; 

// HelloWorld class extends Applet 
public class HelloWorld extends Applet 
// Overriding paint() method 

public void paint(Graphics g) 
g.drawString("Hello World", 20, 20); 


After you enter the source code for, compile in the same way that you have been compiling java programs(using javac command). However, running HelloWorld with the java command will generate an error because it is not an application.

java HelloWorld

Error: Main method not found in class HelloWorld, please define the main method as:
 public static void main(String[] args)


Important points:-

1. All applets are sub-classes (either directly or indirectly) of java.applet.Applet class.
2. Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. JDK provides a standard applet viewer tool called applet viewer.
3. generally, execution of an applet does not begin at main() method.
4. Output of an applet window is not performed by System.out.println(). Rather it is handled with various AWT methods, such as drawString().

Previous Next