简体   繁体   English

创建将在Web浏览器中显示的Java小程序

[英]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. 我正在尝试创建一个基本的Java小程序,它将在Web浏览器中显示我的Java程序的输出。

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. 以前从未使用过applet,我想我将按照一个教程尝试创建一个简单的“ Hello World” applet,以使他们对它们的工作方式有一个简单的了解。

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. 我在以下网址使用示例: http : //www.cs.nccu.edu.tw/~linw/javadoc/tutorial/getStarted/applet/index.html,并严格按照说明进行了操作。

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. 但是,当我编译Java源文件时,尽管在Eclipse的“项目资源管理器”窗口中出现了“ HelloWorld”类文件,但是在Windows资源管理器中查看根项目文件夹时,我根本看不到该类文件-我在那里看到的全部是我的HelloWorld.java文件和Hello.html文件。

When I run the HelloWorld.java class in Eclipse, although I get a warning in the console that says: 当我在Eclipse中运行HelloWorld.java类时,尽管在控制台中收到一条警告:

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

the application does run- and a little window pops up titled "AppletViewer:...HellowWorld.class". 该应用程序会运行,并弹出一个小窗口,标题为“ 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!" 该窗口具有“小程序”菜单,其中包含诸如重新启动,重新加载,停止,保存等菜单项,并且该窗口显示“ Hello World!”。 in the location specified, and a message saying "Applet started." 在指定的位置,并显示一条消息“ Applet已启动”。 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... 但是,当我尝试在浏览器中查看网页时,出现一条消息:“错误。单击以获取详细信息”,其中应显示“ Hello World”消息...

My HelloWorld.java class has the code: 我的HelloWorld.java类具有以下代码:

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是:

<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. 他们的applet在默认包中,而您的applet在openDis.applet包中。

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) dir (目录)
    • applet.html
    • openDis (directory) openDis (目录)
      • applet (directory) applet (目录)
        • HelloWorld.class

The code attribute of the applet tag should not contain the .class extension. applet标记的code属性不应包含.class扩展名。 It must contain the class name only. 它必须仅包含类名称。 You also need to properly specify the codebase attribute of the applet tag. 您还需要正确指定applet标签的codebase属性。 Have a look at the Deploying With the Applet Tag tutorial for details. 有关详细信息,请参阅“ 使用Applet标签进行部署”教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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