为什么小程序在浏览器中没有 运行 而在 IDE 中?

Why doesn't the applet run in browser but in IDE?

我浏览这个网站已经有一段时间了,但这是我的第一个正式 post。

我是 Java 的新手(刚开始使用 applet),我无法在浏览器中将我分配的 applet 添加到 运行。在 Eclipse 中一切 运行 都很好,但是当我打开我的 .html 文件时,只有空白 space。

如果有人可以查看我下面的内容并提供他们的专业知识,我将不胜感激。我确定我犯了一些菜鸟错误并且还没有找到它。谢谢

Java源代码:

// Import necessary classes.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

class Eye extends Thread
{
    public static int mouseXcoordinate;
    public static int mouseYcoordinate;
    private static final int EYEWIDTH = 50;
    private static final int EYEHEIGHT = 75;
    private static final int IRISSIZE = 30;
    private static final int PUPILSIZE = 12;
    private Color irisColor;
    private static final int SMALLXRAD = (EYEWIDTH  - IRISSIZE)/2;
    private static final int SMALLYRAD = (EYEHEIGHT - IRISSIZE)/2;
    private int x, y;
    private double newX, newY;
    private Graphics g;

    // Constructor for a new eye.
    public Eye(int x, int y, Graphics g)
    {
        this.g = g;
        this.x = x;
        this.y = y;

        // Choose random colors for the iris of the eyes.
        irisColor = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
    }

    // Draw the eye, in detail.
    private void draw()
    {
        synchronized(g)
        {
            // Erase any old eye color.
            g.setColor(Color.white);
            g.fillOval(x - EYEWIDTH/2, y - EYEHEIGHT/2, EYEWIDTH, EYEHEIGHT);
            // Draw the iris and set the color.
            g.setColor(irisColor);
            g.fillOval((int)newX - IRISSIZE/2 + 1, (int)newY - IRISSIZE/2 + 1, IRISSIZE, IRISSIZE);
            // Draw the pupil and set the color.
            g.setColor(Color.black);
            g.fillOval((int)newX - PUPILSIZE/2 + 1, (int)newY - PUPILSIZE/2 + 1, PUPILSIZE, PUPILSIZE);
            // Draw the eye outline.
            g.drawOval(x - EYEWIDTH/2, y - EYEHEIGHT/2, EYEWIDTH, EYEHEIGHT);
        }
    }

    // Continuously calculate the current coordinates and redraw the eyes to follow the coordinates.
    public void run()
    {
        for(;;)
        {
            updateCoordinates();
            draw();
            try
            {
                sleep(50);
            }
            catch (InterruptedException e)
            {}
        }

    }

    // Update the mouse coordinates.
    private void updateCoordinates()
    {

        if (mouseXcoordinate == x)
        {
            newX = mouseXcoordinate;

            if (Math.abs(y - mouseYcoordinate) >= SMALLYRAD)
            {
                if ( (y - mouseYcoordinate) > 0 )
                    newY = y - SMALLYRAD;
                else
                    newY = y + SMALLYRAD;
            }
            else
                newY = mouseYcoordinate;
            return;
        }

        // Find intersection point of line to mouse with eye ellipse
        double slope = (double)(mouseYcoordinate - y) / (double)(mouseXcoordinate - x);
        double numerator = SMALLXRAD * SMALLXRAD * SMALLYRAD * SMALLYRAD;
        double denominator = SMALLYRAD * SMALLYRAD + slope * slope * SMALLXRAD * SMALLXRAD;
        newX = Math.sqrt(numerator / denominator);
        newY = slope * newX;

        // Choose appropriate intersection point
        if (mouseXcoordinate < x)
            newX = -Math.abs(newX);
        else
            newX = Math.abs(newX);

        if (mouseYcoordinate < y)
            newY = -Math.abs(newY);
        else
            newY = Math.abs(newY);

        newX += x;
        newY += y;

        if ( (double)(mouseXcoordinate - x)*(mouseXcoordinate - x) / (SMALLXRAD * SMALLXRAD) + (double)(mouseYcoordinate - y)*(mouseYcoordinate - y) / (SMALLYRAD * SMALLYRAD) < 1 )
        {
            newX = mouseXcoordinate;
            newY = mouseYcoordinate;
        }
    }
}

@SuppressWarnings("serial")
public class BurleighWatchMe extends Applet
{
    static final int NUM_EYES = 50;
    Eye[] eyes = new Eye[NUM_EYES];
    int count = -1;
    int width, height;

    // Initializes the applet by loading coordinates and starting two eye threads.
    public void init()
    {
        addMouseMotionListener( new MouseMotionListener()
        {
            public void mouseDragged(MouseEvent e) {}
            public void mouseMoved(MouseEvent e)
            {
                Eye.mouseXcoordinate = e.getX();
                Eye.mouseYcoordinate = e.getY();
                repaint();
            }
        });
    }

    // Starts the eye threads.
    public void start()
    {
        if (count == -1)
        {
            width = getSize().width;
            height = getSize().height;
            final Graphics g = getGraphics( );
            eyes[++count] = new Eye(width/4,   height/2, g);
            eyes[count].start();
            eyes[++count] = new Eye(3*width/4, height/2, g);
            eyes[count].start();
        }

    repaint();
    }

    // Redraws a border around the applet.
    public void update(Graphics g)
    {
        g.drawRect(0,0,width-1,height-1);
    }
}

HTML:

<html>
<head>
    <title>Watch Me Eyes</title>
</head>
<body>
    Move your mouse pointer over these<br />eyes, and watch them follow it!
    <p />
    <applet code="BurleighWatchMe.class" width="400" height="400">
    </applet>
</body>
</html>

在 HTML 代码中,您错过了添加 applet 标签的 codebase 属性。代码库是指包含您的小程序的目录。所以 如果小程序与 HTML 页面不在同一个目录中 那么你必须在 applet 标签内添加 codebase 属性。它将采用以下形式:

<applet>
    codebase="E:\Projects\" code="BurleighWatchMe.class" height="400" width="400"
</applet>

假设 BurleighWatchMe.class 存在于目录 E:\Projects 中。

但是,如果 BurleighWatchMe.class 文件与 HTML 页面存在于同一目录中,并且您仍然看到空白页,这可能是因为您系统上的 JRE 不允许未知的小程序。您需要做的是编辑设置以允许本地小程序。为此,请转到 Start > Configure Java。转到 Java 设置对话框的 Security 选项卡。在 window 的下半部分,您可以看到一个 站点例外列表 。除此之外,还有一个名为 Edit Site List... 的按钮。单击该按钮。您会看到一个网站列表,您可以使用这些网站的小程序 运行。单击 添加 添加新位置。在新字段中,键入 file:/// 并按回车键。它会向您显示警告;点击 继续 。现在单击确定并退出配置 Java 程序。

接下来,重新启动浏览器。当小程序加载时,您会看到一个对话框,其中您必须 select 运行 选项。您现在应该可以 运行 在您的浏览器上使用该小程序了。

请注意 Google Chrome 不再支持小程序。据我所知,唯一支持 applet 的浏览器是 FirefoxInternet Explorer。我测试了它,它在这两个浏览器上工作。

看看能不能解决你的问题。