How to Print Text in a Java Applet
Things You'll Need
Instructions
1Load the NetBeans IDE by clicking on its program icon. When the program loads, navigate to "New/New Project" and select "Java Application" from the list on the right-hand side of the screen. Press "Next," name the project 'HelloWorldApplet,' and click "Finish." A new source code file appears in the NetBeans text editor. The source code file contains an empty main function.
2
Import the libraries necessary for creating Java applets. These libraries contain the code your applet requires to function properly. To import these libraries, write the following at the top of the source code file.
import java.applet.*;
import java.awt.*;
3
Have your class extend the applet class. This causes your class to become a subclass of the applet class and allows you to gain access to all of its functions. When you created a new project in Step 1, the NetBeans IDE automatically created a class declaration for you. This is located near the top of the source code file. Find the text 'public class HelloWorldApplet' and change it to this:
public class HelloWorldApplet extends Applet
4
Create the functions "init" and "stop," which are required for all applets. Write the following statements below the class declaration:
public void init() { }
public void stop() { }
5
Create a paint function. This function will contain code that writes 'Hello World' to the screen. The function drawString prints text at a given set of coordinates. To print the text at X = 20 and Y = 20, write the following code below the previous two function declarations:
public void paint(Graphics g)
{ g.drawString("Hello World!",20,20); }
6Execute your program by pressing the "F6" key.
Source...