[英]Why doesn't the applet run in browser but in IDE?
我浏览该网站已有一段时间,但这是我的第一篇正式帖子。
我对Java还是很陌生(只是开始使用applet),但是我无法让分配的applet在浏览器中运行。 一切在Eclipse中都可以正常运行,但是当我打开.html文件时,只有空白。
如果有人可以看看我下面的内容并提供他们的专业知识,我将不胜感激。 我敢肯定,我已经犯了一些菜鸟错误,还无法找到它。 谢谢。
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
属性的操作。 代码库是指包含小程序的目录。 因此, 如果APPLET与HTML页面不在同一目录中,那么您必须在applet
标签内添加codebase
属性。 格式如下:
<applet>
codebase="E:\Projects\" code="BurleighWatchMe.class" height="400" width="400"
</applet>
假设BurleighWatchMe.class存在于目录E:\\ Projects中 。
但是,如果BurleighWatchMe.class文件与HTML页面位于同一目录中,而您仍看到空白页面,则可能是由于系统上的JRE不允许未知的applet引起的。 您需要做的是编辑设置以允许本地小程序。 为此,请转到Start > Configure Java
。 转到“ Java设置”对话框的“ Security
选项卡。 在窗口的下半部分,您可以看到“ 站点例外列表” 。 除此之外,还有一个名为“ 编辑站点列表...”的按钮。 单击该按钮。 您将看到可以运行其applet的站点列表。 单击添加以添加新位置。 在新字段中,输入file:///
,然后按Enter。 它会向您显示警告; 单击继续 。 现在,单击“确定”并退出“配置Java”程序。
接下来,重新启动浏览器。 加载小程序时,您将看到一个对话框,其中您必须选择“ 运行”选项。 现在,您应该可以在浏览器上运行小程序了。
请注意, Google Chrome浏览器不再支持小程序。 我所知道的唯一支持小程序的浏览 器是Firefox和Internet Explorer 。 我对其进行了测试,并且可以在这两种浏览器上使用。
看看是否能解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.