2013-05-28 74 views
2

你好,我曾嘗試下面的代碼如何使用Web項目

的index.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); 
    } 

} 

當在Eclipse中運行小程序此應用程序在瀏覽器中運行,然後

類未發現異常被觸發..

,但我在瀏覽器

錯誤,如三個按鈕

詳細一個彈出框的問題忽略和重新加載

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文件。

回答

2

你有兩個選擇:
1.使用appletviewer附帶你的JDK只是看到一個最基本的瀏覽器
2.嵌入applet標記在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接受的答案。

相關問題