2012-07-29 114 views
0

當我加載我的HTML頁面時,我的Java applet(稱爲NameInLights.class)不顯示。 .class文件肯定存在,我的HTML的其他部分顯示在頁面上的正確位置。 Java在我的瀏覽器中啓用,並且Applet在AppletViewer中正常工作。Java Applet不顯示在HTML頁面上

這裏是我的Java代碼:

import java.util.Random; 
import java.awt.*; 
import java.applet.*; 
public class NameInLights extends Applet { 

    private static final long serialVersionUID = 1L; 
    Random r = new Random(); 
    String name = "Imulsion";//must keep my real name secret! :) 
    public void init(){ 
     setSize(500,500); 
    } 
    public void paint(Graphics g){ 
    g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); 
    g.setFont(new Font("Comic sans ms",Font.BOLD,55)); 
    g.drawString(name,125,250); 
    for(int loop=0;loop<500;loop++) 
    { 
     int red = r.nextInt(255); 
     int green = r.nextInt(255); 
     int blue = r.nextInt(255); 
     int x = r.nextInt(500); 
     int y = r.nextInt(500); 
     g.setColor(new Color(red,green,blue)); 
     g.setFont(new Font("Times New Roman",Font.BOLD,24)); 
     g.drawString("*",x,y); 
    } 
    } 
} 

,這裏是我的HTML:

<html> 
<body> 
    <p> 
    Here comes an awesome applet! 
    </p> 
    <br /> 
    <br /> 
    <applet code="NameInLights.class" width = 500 height = 500> 
    </applet> 
    <button type = "button" onclick = "window.location.reload()"> 
    Change applet colours 
    </button> 
<body> 
<html> 

爲何不顯示?

+2

您使用什麼瀏覽器? – AlexR 2012-07-29 16:10:54

+0

谷歌瀏覽器。它問我是否應該允許java在頁面上工作,顯然我點擊yes。我試過在Internet Explorer上沒有任何工作 – imulsion 2012-07-29 16:12:16

+0

您是否嘗試清除瀏覽器和Java緩存? – 2012-07-29 16:21:42

回答

1

好吧,谷歌Chrome官方不支持過時的標籤applet。 改爲使用標記object

+0

那麼我的對象標籤看起來像什麼? – imulsion 2012-07-30 06:46:10

+1

我剛剛在簡單的HTML中使用純applet元素測試Google Chrome(版本21.0.1180.60 m),並且它工作得很好。也許你的意思是,Chrome不附帶JRE?這很有道理。瀏覽器不應該附帶所有可以想象的插件,而是用戶應該根據需要安裝它們(HTML提示)。 – 2012-08-05 08:56:05

0

作爲第一個樣子,你可以使用:

appletviewer your_html_file.html 

然後,你可能需要下載一個插件,您的瀏覽器:

http://www.oracle.com/technetwork/java/index-jsp-141438.html 
+0

我用對象標籤,但它仍然劑量工作。繼承人我HTML: – imulsion 2012-08-05 07:23:22

+0

繼承人涼爽小程序


<對象CODETYPE = 「應用/ JAVA」 的classid = 「java的:NameInLights.class」 WIDTH = 「500」 HEIGHT = 「500」> – imulsion 2012-08-05 07:23:40