2

您好我已经尝试了以下代码

索引.jsp

<applet code="com.applet.PrintApplet" codebase ="TestApplet.jar" width="320" height="120"></applet>

java 类PrintApplet.java

import java.applet.Applet;
import java.awt.Color;

public class PrintApplet extends Applet{

    public void paint(Graphics g){
      g.drawString("Welcome in Java Applet.",40,20);
   }

}

当此应用程序在浏览器中运行时

类未找到异常被触发..

但我在浏览器中有问题

错误,例如带有三个按钮的弹出框

细节忽略并重新加载

Application error

classNotFoundException 

com.applet.printApplet

详细信息按钮

Java Plug-in 10.21.2.11
Using JRE version 1.7.0_21-b11 Java HotSpot(TM) Client VM
User home directory = C:\Users\Helthcare2
----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------

我也在构建路径中添加了 jar 文件。

4

1 回答 1

2

您有两种选择:
1. 使用appletviewerJDK 附带的哪个,以便在基本浏览器中查看小程序工作
2. 将小程序标签嵌入您的 HTML 页面。

<html>
    <title>My Applet</title>
    <body>
        <applet code="PrintApplet.class" width="400" height="400"></applet>
    </body>
</html>  

但就目前而言,<applet>已被弃用。
所以,这就是你要做的:
1. 将此添加到您的<head>: <script src="http://java.com/js/deployJava.js"></script>
2. 将其添加到您的<body>:

<script>
    var attributes = {codebase:'http://my.url/my/path/to/codebase',
                      code:'my.main.Applet.class',
                      archive: 'my-archive.jar',
                      width: '800', 
                      height: '600'};
    var parameters = {java_arguments: '-Xmx256m'}; // customize per your needs
    var version = '1.5' ; // JDK version
    deployJava.runApplet(attributes, parameters, version);
</script>  

同样,从 HTML5 开始,<head>不需要,因此您只需键入<script>....</script>

这取自:Embedding Java Applet into .html file accepted answer。

于 2013-05-28T13:29:03.607 回答