2016-03-08 41 views
2

我試着爲我的小程序創建一個html文件,但沒有出現。html不顯示爲Applet

這是代碼到我的小應用程序

public class TimeSet extends Applet{ 

    public TimeSet(){ 
     //set the title 
     frame = new JFrame(); 
     frame.setTitle("2 hour time set"); 

     //specify what happens when the close button is clicked 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //set main panel where all other panels will exist in 
     mainPanel = new JPanel(new GridLayout(4,1)); 
     add(mainPanel); 

     timePanel(); 
     ammountPanel(); 
     durationPanel(); 
     buttonSet(); 

     setVisible(true); 

    } 

,這是我使用我的HTM

<Html> 
<Head> 
<Title>TimeSet</Title> 
</Head> 

<Body> 
<Applet Code="TimeSet.class" width=200 Height=100> 
</Applet> 
</Body> 
</Html> 

代碼是有我的代碼有問題或有一個不同的過程?該html文件與TimeSet.class文件位於同一個文件夾中。當我運行小程序時,它工作正常

+0

根本不要使用小程序。請使用[Java Web Start](http://stackoverflow.com/tags/java-web-start/info)從鏈接啓動框架。 –

回答

0

有兩種入口點方法,init()start(),Java將代表您的Applet調用。在你的情況下,嘗試添加init()方法你的小程序,進而調用您的TimeSet()方法:

public class TimeSet extends Applet{ 

    public void init() { 
     TimeSet(); 
    } 

    public void TimeSet(){ 
     //set the title 
     frame = new JFrame(); 
     frame.setTitle("2 hour time set"); 

     //specify what happens when the close button is clicked 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //set main panel where all other panels will exist in 
     mainPanel = new JPanel(new GridLayout(4,1)); 
     add(mainPanel); 

     timePanel(); 
     ammountPanel(); 
     durationPanel(); 
     buttonSet(); 

     setVisible(true); 
    } 
} 

順便說一句,因爲別人可能會在這裏評論,Applet是一個過時的技術,你不應該計劃在發佈產品中使用它。

+0

它產生一個錯誤,說TimeSet()是未定義的。是的,它的一個類,否則我永遠不會使用它 –

+0

對不起,我省略了'TimeSet()'的返回類型(因爲你也做了,我只是盲目地複製你的代碼)。現在再試一次。 –

+0

html中仍然沒有任何內容 –