简体   繁体   中英

Creating a Java applet that will display in a web browser

I'm trying to create a basic Java applet that will display the output of my Java program in a web browser.

Having never worked with applets before, I thought I would follow a tutorial to try and create a simple "Hello World" applet, just to get a simple understanding of how they work.

I am using the example at: http://www.cs.nccu.edu.tw/~linw/javadoc/tutorial/getStarted/applet/index.html and have followed the steps exactly as described.

However, when I compile the Java source file, although a "HelloWorld" class file appears in my 'Project Explorer' window in Eclipse, I cannot see the class file at all when viewing the root project folder in Windows Explorer- all I see there is my HelloWorld.java file, and Hello.html file.

When I run the HelloWorld.java class in Eclipse, although I get a warning in the console that says:

Warning: Can't read AppletViewer properties file: C:.... Using defaults

the application does run- and a little window pops up titled "AppletViewer:...HellowWorld.class". This window has an 'Applet' menu, with menu items such as Restart, Reload, Stop, Save, etc, and the window displays "Hello World!" in the location specified, and a message saying "Applet started." at the bottom.

But, when I try to view the webpage in a browser, I get a message that says: "Error. Click for details" where the "Hello World" message should be displayed...

My HelloWorld.java class has the code:

package openDis.applet;

import java.awt.Graphics;

public class HelloWorld extends java.applet.Applet {
    public void init() {
        resize(150,25);
    }

    public void paint(Graphics g) {
        g.drawString("Hello world!", 50, 25);
    }
}

and the HTML in the webpage I'm trying to use to display the message is:

<html>
    <head>
        <title>A Simple Program</title>
    </head>
    <body>
        Here is the output of the program:

        <applet code="HelloWorld.class" width=150 height=25></applet>
    </body>
</html>

What am I doing wrong here? What do I need to do to get the output of the program to display in the web page? Thanks for any help in advance!

..have followed the steps exactly as described.

No you didn't. Their applet is in the default package, while yours is in openDis.applet package.

So:

<applet code = "HelloWorld.class" width = 150 height = 25>
</applet>

Should be:

<applet code = "openDis.applet.HelloWorld" width = 150 height = 25>
</applet>

And the structure needs to be:

  • dir (directory)
    • applet.html
    • openDis (directory)
      • applet (directory)
        • HelloWorld.class

The code attribute of the applet tag should not contain the .class extension. It must contain the class name only. You also need to properly specify the codebase attribute of the applet tag. Have a look at the Deploying With the Applet Tag tutorial for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM