Posted on 2007-03-17 10:48
tanzek 閱讀(3209)
評論(0) 編輯 收藏
還是在看Java Tutorial,到了"Applets"。
下面就列出一些看到的重點吧。
An?applet?is?a?special?kind?of?Java?program?that?a?browser?enabled?with?Java?technology?can?download?from?the?internet?and?run.?An?applet?is?typically?embedded?inside?a?web
-
page?and?runs?in?the?context?of?the?browser.?An?applet?must?be?a?subclass?of?the?java.applet.Applet?
class
,?which?provides?the?standard?
interface
?between?the?applet?and?the?browser?environment.
applet是一種特殊的JAVA程序,允許帶有Java技術的瀏覽器能夠從Internet下載它并運行。applet是典型的嵌入到網頁里面并在瀏覽器上下文中運行的。applet必須是java.applet.Applet類的子類,java.applet.Applet提供applet和瀏覽器環境之間的接口標準。
Swing?provides?a?special?subclass?of?Applet,?called?javax.swing.JApplet,?which?should?be?used?
for
?all?applets?that?use?Swing?components?to?construct?their?GUIs.
Swing提供一個特殊的applet子類,叫做javax.swing.JApplet,當所有的applet要用到Swing部件來構造他們的GUI時,那么它就需要用到它。
By?calling?certain?methods,?a?browser?manages?an?applet?life?cycle,?
i
f
?an?applet?is?loaded?in?a?web?page.?
如果applet已經加載到一個網頁中,那么通過調用applet的某一方法,瀏覽器可以控制applet的生命周期。
上面講了applet的一些知識,接下來的就是applet的生命周期的講解了:
方法地地Life?Cycle?of?an?Applet:?Basically,?there?are?four?methods?in?the?Applet?class?on?which?any?applet?is?built.?
Applet的生命周期:基本上,在Applet類的每個實例中有四個方法。
init:?This?method?is?intended?for?whatever?initialization?is?needed?for?your?applet.?It?is?called?after?the?param?attributes?of?the?applet?tag.?
init:這個往往用來執行你的applet需要做的所有初始化工作,當在applet標記中的param屬性讀入后開始調用。
start:?This?method?is?automatically?called?after?init?method.?It?is?also?called?whenever?user?returns?to?the?page?containing?the?applet?after?visiting?other?pages.?
start:此方法在init方法調用完后自動調用,它是執行訪問其它頁面后用戶重新返回到包含applet的頁面中時的操作。
stop:?This?method?is?automatically?called?whenever?the?user?moves?away?from?the?page?containing?applets.?You?can?use?this?method?to?stop?an?animation.?
stop:此方法當用戶從包含applet的頁面中離開時自動調用,你能夠用此方法來停止你的動畫。
destroy:?This?method?is?only?called?when?the?browser?shuts?down?normally.?
destroy:此方法僅當正常地關閉瀏覽器時被調用。
Thus,?the?applet?can?be?initialized?once?and?only?once,?started?and?stopped?one?or?more?times?in?its?life,?and?destroyed?once?and?only?once.
因此,applet僅能用init一次,能夠在它的生命中被start和stop一次或多次,同時也僅能被destroy一次。
最后還用一個例子吧:
?1
/**//*
?2
?*?Java(TM)?SE?6?Version?
?3
?*/
?4
?5
import?java.applet.Applet;
?6
import?java.awt.Graphics;
?7
?8
//No?need?to?extend?JApplet,?since?we?don't?add?any?components;
?9
//we?just?paint.
10
public?class?Simple?extends?Applet?
{
11
12
????StringBuffer?buffer;
13
14
????public?void?init()?
{
15
????????buffer?=?new?StringBuffer();
16
????????addItem("initializing
?");
17
????}
18
19
????public?void?start()?
{
20
????????addItem("starting
?");
21
????}
22
23
????public?void?stop()?
{
24
????????addItem("stopping
?");
25
????}
26
27
????public?void?destroy()?
{
28
????????addItem("preparing?for?unloading
");
29
????}
30
31
????private?void?addItem(String?newWord)?
{
32
????????System.out.println(newWord);
33
????????buffer.append(newWord);
34
????????repaint();
35
????}
36
37
????public?void?paint(Graphics?g)?
{
38
????//Draw?a?Rectangle?around?the?applet's?display?area.
39
????????g.drawRect(0,?0,?
40
???????????getWidth()?-?1,
41
???????????getHeight()?-?1);
42
43
????//Draw?the?current?string?inside?the?rectangle.
44
????????g.drawString(buffer.toString(),?5,?15);
45
????}
46
}