Posted on 2007-03-17 10:48
tanzek 閱讀(3214)
評(píng)論(0) 編輯 收藏
還是在看Java Tutorial,到了"Applets"。
下面就列出一些看到的重點(diǎn)吧。
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技術(shù)的瀏覽器能夠從Internet下載它并運(yùn)行。applet是典型的嵌入到網(wǎng)頁(yè)里面并在瀏覽器上下文中運(yùn)行的。applet必須是java.applet.Applet類的子類,java.applet.Applet提供applet和瀏覽器環(huán)境之間的接口標(biāo)準(zhǔn)。
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提供一個(gè)特殊的applet子類,叫做javax.swing.JApplet,當(dāng)所有的applet要用到Swing部件來(lái)構(gòu)造他們的GUI時(shí),那么它就需要用到它。
By?calling?certain?methods,?a?browser?manages?an?applet?life?cycle,?
i
f
?an?applet?is?loaded?in?a?web?page.?
如果applet已經(jīng)加載到一個(gè)網(wǎng)頁(yè)中,那么通過(guò)調(diào)用applet的某一方法,瀏覽器可以控制applet的生命周期。
上面講了applet的一些知識(shí),接下來(lái)的就是applet的生命周期的講解了:
方法地地Life?Cycle?of?an?Applet:?Basically,?there?are?four?methods?in?the?Applet?class?on?which?any?applet?is?built.?
Applet的生命周期:基本上,在Applet類的每個(gè)實(shí)例中有四個(gè)方法。
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:這個(gè)往往用來(lái)執(zhí)行你的applet需要做的所有初始化工作,當(dāng)在applet標(biāo)記中的param屬性讀入后開(kāi)始調(diào)用。
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方法調(diào)用完后自動(dòng)調(diào)用,它是執(zhí)行訪問(wèn)其它頁(yè)面后用戶重新返回到包含applet的頁(yè)面中時(shí)的操作。
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:此方法當(dāng)用戶從包含applet的頁(yè)面中離開(kāi)時(shí)自動(dòng)調(diào)用,你能夠用此方法來(lái)停止你的動(dòng)畫(huà)。
destroy:?This?method?is?only?called?when?the?browser?shuts?down?normally.?
destroy:此方法僅當(dāng)正常地關(guān)閉瀏覽器時(shí)被調(diào)用。
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一次或多次,同時(shí)也僅能被destroy一次。
最后還用一個(gè)例子吧:
?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
}