J2EEOnlineJ2EE





Java Servlets   «Prev 

Built and Test Java Applet - Exercise

Objectives for this Lesson

Build and test a Java applet.
This exercise is to make sure that you have the JDK installed correctly.
Click the Submitbutton below when finished.

Instructions

  1. Using the Java tools you already have installed (for example, the JDK and a text editor) build a Java applet that draws the text “Hello, World!” on its surface.
    1. Compile the applet.
    2. Write the HTML required to include the applet in a Web page.
    3. Hint: You should create a new file called HelloWorldApplet.java.
    4. Load the HTML into a browser and confirm that you see the Hello, World! message.

    It should contain the following code:
    
    import java.applet.*;
    import java.awt.*;
    
    public class HelloWorldApplet extendsApplet{
      public voidpaint(Graphics g) {
       g.drawString("Hello, World!",20,20);
      }
    }
    

  2. Save the file, and compile it with the JDK Java compiler. Fix any errors in your typing that the compiler found.
  3. Create another file called HelloWorld.htm, in the same subdirectory or folder as your applet source. It should contain the following HTML (you can copy this and paste it in to your text editor):
  4. <html>
    <head>
    <title>Hello World Applet</title>
    </head>
    <body bgcolor=#FFFFFF>
    <applet code="HelloWorldApplet.class"
    height=200 width=200>
    </applet>
    </body>
    </html>
    
  5. Save the HTML, then open it in a Web browser.