简体   繁体   English

为什么小程序不能在浏览器中运行,而不能在IDE中运行?

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

I've been browsing this site for some time, but this is my first official post. 我浏览该网站已有一段时间,但这是我的第一篇正式帖子。

I'm fairly new to Java (just getting started with applets), and I'm having trouble getting my assigned applet to run in a browser. 我对Java还是很陌生(只是开始使用applet),但是我无法让分配的applet在浏览器中运行。 Everything runs fine in Eclipse, but there is nothing but blank space when I open my .html file. 一切在Eclipse中都可以正常运行,但是当我打开.html文件时,只有空白。

I'd greatly appreciate if anyone could look over what I have below and offer their expertise. 如果有人可以看看我下面的内容并提供他们的专业知识,我将不胜感激。 I'm sure I've made some noob mistake and haven't been able to locate it yet. 我敢肯定,我已经犯了一些菜鸟错误,还无法找到它。 Thanks. 谢谢。

Java source code: 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:

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

In the HTML code, you've missed out on adding the codebase attribute of the applet tag. 在HTML代码中,您错过了添加applet标签的codebase属性的操作。 Codebase refers to the directory containing your applet. 代码库是指包含小程序的目录。 So IF THE APPLET IS NOT IN THE SAME DIRECTORY AS THE HTML PAGE then you have to add the codebase attribute inside the applet tag. 因此, 如果APPLET与HTML页面不在同一目录中,那么您必须在applet标签内添加codebase属性。 It would take the form below: 格式如下:

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

assuming that the BurleighWatchMe.class exists in the directory E:\\Projects . 假设BurleighWatchMe.class存在于目录E:\\ Projects中

However, if the BurleighWatchMe.class file exists in the same directory as the HTML page and you're still seeing a blank page, then it probably might have been caused because the JRE on your system doesn't allow unknown applets. 但是,如果BurleighWatchMe.class文件与HTML页面位于同一目录中,而您仍看到空白页面,则可能是由于系统上的JRE不允许未知的applet引起的。 What you have to do is edit the settings to allow local applets. 您需要做的是编辑设置以允许本地小程序。 To do that, go to Start > Configure Java . 为此,请转到Start > Configure Java Go to the Security tab of the Java settings dialog box. 转到“ Java设置”对话框的“ Security选项卡。 In the lower half of the window, you can see a Site Exception List . 在窗口的下半部分,您可以看到“ 站点例外列表” Beside that, there's a button called Edit Site List... . 除此之外,还有一个名为“ 编辑站点列表...”的按钮。 Click on that button. 单击该按钮。 You'll see a list of sites whose applets you can run. 您将看到可以运行其applet的站点列表。 Click on Add to add a new location. 单击添加以添加新位置。 In the new field, type file:/// and press enter,. 在新字段中,输入file:/// ,然后按Enter。 It'll show you a warning; 它会向您显示警告; click on Continue . 单击继续 Now click on OK and quit the Configure Java program. 现在,单击“确定”并退出“配置Java”程序。

Next, restart the browser. 接下来,重新启动浏览器。 You'll see a dialog box when the applet loads wherein you have to select the Run option. 加载小程序时,您将看到一个对话框,其中您必须选择“ 运行”选项。 You should now be able to run the applet on your browser. 现在,您应该可以在浏览器上运行小程序了。

Please note that Google Chrome no longer supports applets. 请注意, Google Chrome浏览器不再支持小程序。 The only browsers I know of that support applets are Firefox and Internet Explorer . 我所知道的唯一支持小程序的浏览 FirefoxInternet Explorer I tested it and it worked on these two browsers. 我对其进行了测试,并且可以在这两种浏览器上使用。

See if it solves your problem. 看看是否能解决您的问题。

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

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