你好世界,Java Applet,问题
我在使用 HelloWorld Applet 时遇到问题。
这是我的 Java 代码:
package webCrawler.applet2;
import javax.swing.JApplet;
import java.awt.Graphics;
public class HappyFace extends JApplet
{
public void paint (Graphics canvas)
{
canvas.drawOval(100,50,200,200);
canvas.fillOval(155,100,10,20);
canvas.fillOval(230,100,10,20);
canvas.drawArc(150,160,100,50,0,180);
}
}
这是我的 index.html
:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="000000">
<center>
<applet
code = "HappyFace.class"
width = "500"
height = "300"
>
</applet>
</center>
</body>
</html>
在 Eclipse 中,如果我执行以下操作: Run ->运行
它可以工作,但是如果我这样做:
% pwd
/Users/me/Documents/workspace/WebCentric/bin/webCrawler/applet2
% ls
HappyFace.class index.html
% open index.html
它会在 Firefox 中打开 html 页面,但应用程序无法工作:
更新:
- 更改了index.html的位置
- 将index.html更改为
code =“webCrawler.applet2.HappyFace”
这是控制台中的异常。
java.lang.UnsupportedClassVersionError: webCrawler/applet2/HappyFace (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:213)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:151)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:680)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:635)
at sun.plugin.AppletViewer.createApplet(AppletViewer.java:1991)
at jep.AppletFramePanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:564)
at sun.applet.AppletPanel.run(AppletPanel.java:301)
at jep.AppletFramePanel.run(Unknown Source)
at java.lang.Thread.run(Thread.java:552)
I am having trouble with a HelloWorld Applet.
Here is my Java code:
package webCrawler.applet2;
import javax.swing.JApplet;
import java.awt.Graphics;
public class HappyFace extends JApplet
{
public void paint (Graphics canvas)
{
canvas.drawOval(100,50,200,200);
canvas.fillOval(155,100,10,20);
canvas.fillOval(230,100,10,20);
canvas.drawArc(150,160,100,50,0,180);
}
}
Here is my index.html
:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="000000">
<center>
<applet
code = "HappyFace.class"
width = "500"
height = "300"
>
</applet>
</center>
</body>
</html>
In Eclipse if I go: Run -> Run
it works, however if I do this:
% pwd
/Users/me/Documents/workspace/WebCentric/bin/webCrawler/applet2
% ls
HappyFace.class index.html
% open index.html
It opens the html page in Firefox but the app does not work:
Update:
As Pablo Santa Cruz suggested I:
- Changed the location of index.html
- Changed index.html to be
code = "webCrawler.applet2.HappyFace"
This is the exception in the console.
java.lang.UnsupportedClassVersionError: webCrawler/applet2/HappyFace (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:213)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:151)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:680)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:635)
at sun.plugin.AppletViewer.createApplet(AppletViewer.java:1991)
at jep.AppletFramePanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:564)
at sun.applet.AppletPanel.run(AppletPanel.java:301)
at jep.AppletFramePanel.run(Unknown Source)
at java.lang.Thread.run(Thread.java:552)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
applet
标记的code
属性中,您应该使用该类的 FQN:webCrawler.applet2.HappyFace
(删除.class
扩展名也)。另外,请确保您的
.class
文件位于适当的目录(包)结构中。在您的情况下,HTML 文件应该位于:并且只有您的 HappyFace.class 文件应该位于:
另外,请记住,浏览器(IE、Firefox、Chrome、Safari)上有一个 Java 控制台,您可以采取查看小程序执行时遇到的错误是什么。
更新:
您的异常表明您使用比 JVM 支持更新的 Java 版本编译代码。 IE,您在 Eclipse 中使用了 Java 6 编译器,但您的 Java 浏览器插件仅为 Java 5(您的 JRE 仅为 Java 5)强>)。
我会更改 Eclipse 中的项目设置,告诉它使用 Java 5 兼容编译器(有一个选项),再次编译和部署 Applet。它应该有效。
In your
code
attribute in theapplet
tag, you should use FQN of the class:webCrawler.applet2.HappyFace
(remove.class
extension also).Also, make sure you have your
.class
file inside the appropriate directory (package) structure. In your case, the HTML files should be in:And only your HappyFace.class file should be in:
Also, do keep in mind that there is a Java console on the Browser (IE, Firefox, Chrome, Safari) you can take a look at to see what's the error you are getting on applet execution.
UPDATE:
Your Exception is saying that you compiled your code with a newer version of Java than the JVM supports. I.E. you used Java 6 compiler in Eclipse but your Java Browser Plugin is only Java 5 (your JRE is only Java 5).
I would change project settings in Eclipse, tell it to use Java 5 compatible compiler (there is an option for that), compile and deploy the Applet again. It should work.