Tải bản đầy đủ - 0 (trang)
Chapter 6. GUI Programming: From Appletsto Swing

Chapter 6. GUI Programming: From Appletsto Swing

Tải bản đầy đủ - 0trang

.

.

Inanapplet,theinitmethodletsyouinitializetheapplet,and

we'llsetthebackgroundcolortowhiteinthatmethod.The

start,stop,anddestroymethodsletyouhandlethe

associatedeventsintheapplet'slifecycle.Todrawourtextin

theapplet'swindow,we'llusetheGraphicsobjectpassedto

thepaintmethod.Here'sthecodetoadd:

publicclassCh06_01extendsApplet{



publicvoidinit()

{

setBackground(Color.white);

}



publicvoidstart()

{

}



publicvoidpaint(Graphicsg)

{



g.drawString("HellofromEclipse!",60,100);

}



publicvoidstop()

{

}



publicvoiddestroy()

{

}

}

Andthat'sallyouneedfortheapplet'scode.Totestit,select

RunAs JavaApplet,andtheappletshouldrun.IfEclipse

doesn'trecognizeyourapplet,youcanspecificallysetthe

launchconfigurationforthiscode.Todothat,selectthe

Ch06_01projectinthePackageExplorerandselectRun Run

toopentheRundialogyouseeinFigure6-1.Thendouble-click

theJavaAppletconfigurationintheConfigurationsboxandthe

Ch06_01projectwillautomaticallybeaddedasanapplet.Click

theRunbuttontoruntheapplet.Aftersettingthelaunch

configuration,youshouldbeabletoselectRunAs JavaApplet

toruntheapplet.



Figure6-1.Settinganapplet'slaunch



configuration



YoucanseetheresultsintheJavaappletviewerinFigure6-2.

Theappletisworkingasitshould.



Figure6-2.Runninganapplet



Tousethisappletinawebpage,usethegeneratedclassfile,

Ch06_01.class,togetherwiththeHTMLtag,somethinglike

this:


CODE="Ch06_01.class"

WIDTH=300

HEIGHT=200



>



That'sallittakes;theappletiscomplete.Youcanseetheentire

codefortheappletinExample6-1.



Example6-1.Asimpleapplet

packageorg.eclipsebook.ch06;



importjava.applet.Applet;

importjava.awt.*;



publicclassCh06_01extendsApplet{

publicvoidinit()

{

setBackground(Color.white);

}



publicvoidstart()

{



}



publicvoidpaint(Graphicsg)

{

g.drawString("HellofromEclipse!",60,100);

}



publicvoidstop()

{

}



publicvoiddestroy()

{

}

}

Whenyoulaunchthem,JavaappletsappearintheJavaapplet

viewerbydefault.That'sfineasfarasitgoes,butthere'smore

toGUIsthanapplets.Thenextstepisgoingtobecreatingour

ownwindowedapplications.



6.1CreatingAWTApplications

TheAbstractWindowingToolkit,orAWT,wasJava'searly

attemptataGUItoolkit.It'sstillsupportedandused,sowe'll

takealookatanexampleherethatwilllaunchitsownwindow.

TheoriginalAWTpackageonlytooksixweekstowrite,andthe

controls,designedforuseinapplets,weremodeledafterHTML

controls.Sincethattime,Swinghastakenoverthestandard

JavainterfacebutevenSwingisbuiltontopofAWT.

OurAWTexamplewilljustlaunchitsownwindowanddisplay

thesamemessageasourapplet.Tofollowalong,createanew

project,Ch06_02,andgiveitanewclasswithamainmethod,

Ch06_02,puttingthatclassintotheorg.eclipsebook.ch06

package.Weneedtoimportjava.awt.*forbasicAWT

support,andjava.awt.event.*tohandlethewindow-closing

event,soaddthiscode:

importjava.awt.*;

importjava.awt.event.*;

Theactualwindowthisexamplewilldisplaywillbebasedonthe

AWTFrameclassandwillbecalledAppFrame,soaddthiscode

toCh06_02.java:

classAppFrameextendsFrame

{

.

.

.



}

Asinourapplet,we'lloverridethepaintmethodandusethe

passedGraphicsobjecttodisplaythetextwewantinthenew

window:

classAppFrameextendsFrame

{

publicvoidpaint(Graphicsg)

{

g.drawString("HellofromEclipse!",60,100);

}

}

Now,intheCh06_02class'smainmethod,we'llcreateanew

windowoftheAppFrameclassandsetitssize:

publicclassCh06_02{



publicstaticvoidmain(String[]args)

{

AppFramef=newAppFrame();



f.setSize(200,200);

.



.

.

}

}

Allthat'sleftistoaddthestandardcodetohandlethewindowcloseeventthatoccurswhentheuserclickstheXbuttoninthe

windowattheupperright,andtoshowthewindowusingits

showmethod:

publicclassCh06_02{



publicstaticvoidmain(String[]args)

{

AppFramef=newAppFrame();



f.setSize(200,200);



f.addWindowListener(newWindowAdapter(){publicvoid

windowClosing(WindowEvente){System.exit(0);}});



f.show();



}

}

Torunthisapplication,selectRunAs JavaApplication.This

applicationwilllaunchtheAWTwindow,completewiththe

messageyouseeinFigure6-3.



Figure6-3.RunningawindowedAWTapplication



Andthat'sitthecodeforthisapplicationappearsinExample62.



Example6-2.AbasicAWTapplication

packageorg.eclipsebook.ch06;



importjava.awt.*;

importjava.awt.event.*;



publicclassCh06_02{



publicstaticvoidmain(String[]args)



{

AppFramef=newAppFrame();



f.setSize(200,200);



f.addWindowListener(newWindowAdapter(){publicvoid

windowClosing(WindowEvente){System.exit(0);}});



f.show();

}

}



classAppFrameextendsFrame

{

publicvoidpaint(Graphicsg)

{

g.drawString("HellofromJava!",60,100);

}



}

Asyoucansee,launchingwindowedapplicationsthiswayisno

problemfromEclipsejustsetupyourapplicationtolaunch

windowsandrunit.AWTisfineuptoapoint,butSwingis

wheretheactionistoday.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 6. GUI Programming: From Appletsto Swing

Tải bản đầy đủ ngay(0 tr)

×