Tải bản đầy đủ - 0 (trang)
Chapter 7. SWT: Buttons, Text, Labels, Lists, Layouts, and Events

Chapter 7. SWT: Buttons, Text, Labels, Lists, Layouts, and Events

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

7.1JavaGraphics

GraphicsworkinJavahasalongandgloriouspast,and,let's

hope,asimilarfuture.ItstartedwiththeverybasicAWT,

movedonthroughthepowerfulSwingpackage,andnowstands

onthethresholdoftheSWTage.



7.1.1AWT

TheAWT,Java'sfirstattemptataGUItoolkit,waswrittenina

matterofweeks.ItletsJavadevelopersdisplaywindowswith

variouscontrolsliketextboxesandbuttons.AWTGUIswere

easytodevelop,andtheyusedtheunderlyingoperating

system'scontrolsthemselvesforexample,inWindows,you'd

seeaWindowstextbox.OntheMac,you'dseeaMactextbox.

Someoperatingsystemshadadifferentcontrolsetfromother

operatingsystems,whichmeantthatSunonlyimplemented

thosecontrolscommontoalloperatingsystemsJavawas

targetedto,andthatlimitedAWTtoasetofrelativelysimple

controls.



7.1.2Swing

Toaddressthegrowingneedsofdevelopers,Javaintroduced

Swing,whichprovidesnon-nativeimplementationsofhigher

levelcontrolsliketrees,tables,andtext.Thisprovidesagreat

dealoffunctionality,butitmakesapplicationsdevelopedin

SwingstandoutasbeingdifferentandveryJava-specific.

Sunaddeda"look-and-feel"emulation(whichwesawatwork

inthepreviouschapter)tohelpapplicationslookmorelikethe

operatingsystemthey'rerunningon,buttheycouldn'tkeepup



withalltheoperatingsystemreleases(suchasWindowsME,

2000,XP,andsoon).Inaddition,becausetheGUIwas

implementedinJavaandnotnativelyintheoperatingsystem,

Swingresponsetimewaspoorcomparedtonativeapplications.



7.1.3SWT

SWTaddressesmanyoftheissuesherebyprovidingasetof

widgetsthatmakeuseofnativecontrols(throughtheJava

NativeInterface,JNI)whensuchcontrolsareavailable.

Otherwise,SWTcreatesitsowncontrolsasneededforthose

thatdon'thaveanoperatingsystemcounterpart.Thisdoes

meanthatnativecodeisneededtosupporteachoperating

system,butsofar,IBMhasbeenabletoprovidethatandstay

up-to-date.Additionally,SWTcomeswithEclipse,soitisafit

topicforourdiscussion.

SWTissubstantialandfeature-rich,asyou'dexpectifit's

intendedtoreplaceAWTandSwing.We'regoingtogetan

introductiontoSWTinthisandthenextchapter,anditturns

outthatyoureallydon'tneedin-depthSWTknowledgeto

createpowerfulandusefulSWTapplications.Allyouneedto

knowaboutarewidgets(theuser-interfaceelementsyouusein

SWT),SWTlayouts(whichletyoupositionthosewidgets),and

SWTevents.We'regoingtoseeplentyofexamples,starting

immediately,toseehowalltheseworkinSWT.



SWTisbeingportedtomoreandmoregraphicalenvironmentsallthe

time.Currentlysupportedare:Windows,LinuxGTK,LinuxMotif,Solaris

Motif,AIXMotif,HPUXMotif,PhotonQNX,andMacOSX.



7.2AnSWTExample

InsteadoftalkingaboutSWTintheabstract,let'sgetthisshow

ontheroadandseesomecodeatwork.Codinganexampleis

goingtogiveustheSWTstoryandwhatittakestoput

togetheranSWTapplication.Ourfirstexamplewillbeasimple

one,anditwilljustdisplaythemessage"Noworries!"inan

SWTwindow.

Tofollowalong,createanewproject,Ch07_01,andaddaclass,

Ch07_01,intheorg.eclipsebook.ch07package.Toworkwith

SWTandSWTwidgets,youtypicallystartwiththesetwo

imports:

importorg.eclipse.swt.widgets.*;

importorg.eclipse.swt.*;

You'llneedtoincludeswt.jarinthebuildpathtomakethese

importswork.RememberthatSWTisoperatingsystemdependent,sothere'sgoingtobeadifferentswt.jarfor

differentoperatingsystems.Toaddswt.jartotheCh07_01

project,selectthatprojectinthePackageExplorer,right-clickit,

andselectProperties.InthePropertiesforCh07_01dialogthat

opens,selecttheJavaBuildPathitemandclicktheAdd

ExternalJARsbutton.Thennavigatetoswt.jar,whichyou'llfind

inoneofthefollowingdirectories,dependingonyouroperating

system(notethatINSTALLDIRistheEclipseinstallation

directory;alsonotethatyou'llhavetoupdatethesepathsfor

yourversionofEclipse,suchaschanging2.1.1to2.1.2orsome

othervalue):



Win32



INSTALLDIR\eclipse\plugins\org.eclipse.swt.win32_2.1.1\ws\win32\sw



LinuxGTK

INSTALLDIR/eclipse/plugins/org.eclipse.swt.gtk_2.1.1/ws/gtk/swt.jar



LinuxMotif



INSTALLDIR/eclipse/plugins/org.eclipse.swt.motif_2.1.1/ws/motif/swt.



SolarisMotif



INSTALLDIR/eclipse/plugins/org.eclipse.swt.motif_2.1.1/ws/solaris/spa



AIXMotif



INSTALLDIR/eclipse/plugins/org.eclipse.swt.motif_2.1.1/ws/aix/ppc/sw



HPUXMotif



INSTALLDIR/eclipse/plugins/org.eclipse.swt.motif_2.1.1/ws/hpux/PA_R



PhotonQNX



INSTALLDIR/eclipse/plugins/org.eclipse.swt.photon_2.1.1/ws/photon/s



MacOSX



INSTALLDIR/eclipse/plugins/org.eclipse.swt.carbon_2.1.1/ws/carbon/s

Afterlocatingswt.jar,clickOpen,andthenclickOKtoadditto

theproject'sbuildpath.



Someoperatingsystems,suchasLinuxGTK,needmorethanoneJAR

torunSWT(inLinuxGTK,youuseswt.jarandswt-pi.jar).Insuch

cases,youhavetoaddalloftherequiredJARstothebuildpath.(All

therequiredJARfileswillbeinthesamefolder.)



Inthecode,thenextstepistocreateaDisplayobject,which

representsanSWTsession.Thisobjectactsastheconnection

betweenSWTandtheoperatingsystem'sGUIsupport.Youuse

aDisplayobjecttostartaneventloopandcontrol

communicationbetweenthemainuserinterfacethreadand

otherthreads.Here'showwecreatetheDisplayobjectinthe

mainmethodoftheCh07_01class:

importorg.eclipse.swt.widgets.*;

importorg.eclipse.swt.*;



publicclassCh07_01{



publicstaticvoidmain(String[]args){

Displaydisplay=newDisplay();



.

.

.

}

Nowthatyou'vegotaDisplayobject,youcancreatethe

variouswindowsyouwanttoworkwithbycreatingShell

objects.InSWT,ashellisawindowthatismanagedbythe

operatingsystem'swindowmanager.Atop-levelshellisone

thatisadirectchildofthedisplayandisawindowtheusercan

move,resize,minimize,andsoon.Youcanalsohavesecondary

shells,whicharechildrenofanothershell,suchasdialogsor

messageboxes.Here'showwecreateandsizetheshellwe'llbe

usingnotethatpassingthedisplayobjecttotheShell

constructormakestheshellachildofthedisplayobject:

importorg.eclipse.swt.widgets.*;

importorg.eclipse.swt.*;



publicclassCh07_01{



publicstaticvoidmain(String[]args){

Displaydisplay=newDisplay();

Shellshell=newShell(display);

shell.setSize(300,200);



.

.

.

}

}

We'rereadytoaddsomewidgetstoourapplication.You'llfind

that,intheSWTdocumentation,thetermwidgetisusedalmost

interchangeablywiththetermscontrolandcomposite.

Technicallyspeaking,theformalandsomewhatcirculardefinition

ofawidgetintheSWTdocumentationis"theabstractclassfor

anyUIobjectthatcanbeplacedinsideanotherwidget."

Practicallyspeaking,widgetisthegeneraltermforanyUI

elementinSWT.Compositesarewidgetsthataredesignedto

havechildren,suchastoolbars,trees,andcanvases.Controls

arewidgetsthathaveanoperatingsystemcounterpart,suchas

buttons,lists,andlabels.YoucanseealltheSWTcontrolsin

Table7-1.Notethatwe'realsolistingthepossiblestylesand

eventsforeachcontrol.

Table7-1.SWTcontrolsandoptions

Widget



Button



Purpose



Styles



Events



Astandardpush

button.



BORDER,CHECK,PUSH,

RADIO,TOGGLE,FLAT,

LEFT,RIGHT,CENTER,

ARROW(withUP,DOWN)



Dispose,FocusIn,

FocusOut,Help,

KeyDown,KeyUp,

MouseDoubleClick,

MouseDown,

MouseEnter,MouseExit,

MouseHover,MouseUp,

MouseMove,Move,

Paint,Resize,Selection



Acompositecontrol

thatcancontainother



BORDER,H_SCROLL,

V_SCROLL,

NO_BACKGROUND,



Dispose,FocusIn,

FocusOut,Help,

KeyDown,KeyUp,

MouseDoubleClick,



Canvas



Caret



Combo



controls,andalsoisa

drawingsurface.Often

thefoundationof

customcontrols.



NO_FOCUS,

NO_MERGE_PAINTS,

NO_REDRAW_RESIZE,

NO_RADIO_GROUP



MouseDown,

MouseEnter,MouseExit,

MouseHover,MouseUp,

MouseMove,Move,

Paint,Resize



ThestandardI-beam

caretusedtoindicate

theinsertionpointfor

text.







Dispose



BORDER,DROP_DOWN,

READ_ONLY,SIMPLE



Dispose,FocusIn,

FocusOut,Help,

KeyDown,KeyUp,

MouseDoubleClick,

MouseDown,

MouseEnter,MouseExit,

MouseHover,MouseUp,

MouseMove,Move,

Paint,Resize,

DefaultSelection,Modify,

Selection



Astandardcombo

boxthatis,the

combinationofatext

controlandadropdownlist.



Controlthatcancontain BORDER,H_SCROLL,

otherwidgets.

V_SCROLL



Dispose,FocusIn,

FocusOut,Help,

KeyDown,KeyUp,

MouseDoubleClick,

MouseDown,

MouseEnter,MouseExit,

MouseHover,MouseUp,

MouseMove,Move,

Paint,Resize



CoolBar



Acompositecontrol

thatallowsusersto

repositioncontained

itemsdynamically.



BORDER



Dispose,FocusIn,

FocusOut,Help,

KeyDown,KeyUp,

MouseDoubleClick,

MouseDown,

MouseEnter,MouseExit,

MouseHover,MouseUp,

MouseMove,Move,

Paint,Resize



CoolItem



AUIelementthat

representsa

positionableareaofa

coolbar.



DROP_DOWN



Dispose



Composite



Dispose,FocusIn,



Group



Label



FocusOut,Help,

KeyDown,KeyUp,

MouseDoubleClick,

MouseDown,

MouseEnter,MouseExit,

MouseHover,MouseUp,

MouseMove,Move,

Paint,Resize



Acompositecontrol

thatgroupsother

widgetstogether.Can

enclosetheminan

etchedborderand

displayalabel.



BORDER,

SHADOW_ETCHED_IN,

SHADOW_ETCHED_OUT,

SHADOW_IN,

SHADOW_OUT,

SHADOW_NONE



Thestandardlabel

controldisplaystextor

animage.



Dispose,FocusIn,

FocusOut,Help,

BORDER,CENTER,LEFT,

KeyDown,KeyUp,

RIGHT,WRAP,SEPARATOR

MouseDoubleClick,

(withHORIZONTAL,

MouseDown,

SHADOW_IN,

MouseEnter,MouseExit,

SHADOW_OUT,

MouseHover,MouseUp,

SHADOW_NONE,VERTICAL)

MouseMove,Move,

Paint,Resize

Dispose,FocusIn,

FocusOut,Help,

KeyDown,KeyUp,

MouseDoubleClick,

MouseDown,

MouseEnter,MouseExit,

MouseHover,MouseUp,

MouseMove,Move,

Paint,Resize,Selection,

DefaultSelection



List



Thestandardlist

controlallowstheuser BORDER,H_SCROLL,

tochooseitemsfroma V_SCROLL,SINGLE,MULTI

listofitems.



Menu



Thestandardmenu

BAR,DROP_DOWN,

Dispose,Help,Hide,

controlcontains

NO_RADIO_GROUP,POP_UP Show

selectablemenuitems.



MenuItem



UIobjectthat

CHECK,CASCADE,PUSH,

representsanitemina

RADIO,SEPARATOR

menu.



Dispose,Arm,Help,

Selection



Dispose,FocusIn,

FocusOut,Help,

KeyDown,KeyUp,

Thestandardprogress BORDER,INDETERMINATE,

MouseDoubleClick,

bardisplaysprogressof SMOOTH,HORIZONTAL,

MouseDown,

ProgressBar atasktotheuser,

VERTICAL

MouseEnter,MouseExit,

usuallyasabargraph.

MouseHover,MouseUp,

MouseMove,Move,

Paint,Resize



Allowstheusertodrag



Dispose,FocusIn,

FocusOut,Help,



Sash



a"rubber-banded"

outlineofthesash

withintheparent

windowtoallowusers

toresizechildwidgets

bymovingtheir

dividinglines.



BORDER,HORIZONTAL,

VERTICAL



KeyDown,KeyUp,

MouseDoubleClick,

MouseDown,

MouseEnter,MouseExit,

MouseHover,MouseUp,

MouseMove,Move,

Paint,Resize,Selection



Scale



Controlthatrepresents

BORDER,HORIZONTAL,

arangeofnumeric

VERTICAL

values.



Dispose,FocusIn,

FocusOut,Help,

KeyDown,KeyUp,

MouseDoubleClick,

MouseDown,

MouseEnter,MouseExit,

MouseHover,MouseUp,

MouseMove,Move,

Paint,Resize,Selection



ScrollBar



Thestandard

scrollbarrepresentsa

rangeofpositive

numericvalues.



HORIZONTAL,VERTICAL



Dispose,Selection



Awindowthatis

managedbythe

operatingsystem

windowmanager.



Dispose,FocusIn,

FocusOut,Help,

KeyDown,KeyUp,

MouseDoubleClick,

BORDER,H_SCROLL,

MouseDown,

V_SCROLL,CLOSE,MIN,

MouseEnter,MouseExit,

MAX,NO_TRIM,RESIZE,

MouseHover,MouseUp,

TITLE(seealso

MouseMove,Move,

SHELL_TRIM,DIALOG_TRIM)

Paint,Resize,Activate,

Close,

Deactivate,Deiconify,

Iconify



Shell



Slider



Controlthatrepresents

arangeofnumeric

values,whichtheuser BORDER,HORIZONTAL,

canselectby

VERTICAL

positioningadraggable

thumb.



Compositecontrolthat

groupscontrolsinto



Dispose,FocusIn,

FocusOut,Help,

KeyDown,KeyUp,

MouseDoubleClick,

MouseDown,

MouseEnter,MouseExit,

MouseHover,MouseUp,

MouseMove,Move,

Paint,Resize,Selection



Dispose,FocusIn,

FocusOut,Help,

KeyDown,KeyUp,

MouseDoubleClick,



TabFolder



TabItem



Table



pagesthattheusercan BORDER

selectusinglabeled

tabs.



Controlcorresponding

toatabinatabfolder.



Controlthatdisplaysa

listoftableitems.



Text



ToolBar



UIobjectthat

representsanitemina

table.



Thestandardtext

control,whichallows

theusertotypetext

intoit.



Dispose



Dispose,FocusIn,

FocusOut,Help,

KeyDown,KeyUp,

BORDER,H_SCROLL,

MouseDoubleClick,

V_SCROLL,SINGLE,MULTI, MouseDown,

CHECK,FULL_SELECTION, MouseEnter,MouseExit,

HIDE_SELECTION

MouseHover,MouseUp,

MouseMove,Move,

Paint,Resize,Selection,

DefaultSelection



UIobjectthat

TableColumn representsacolumnin LEFT,RIGHT,CENTER

atable.



TableItem



MouseDown,

MouseEnter,MouseExit,

MouseHover,MouseUp,

MouseMove,Move,

Paint,Resize,Selection



Dispose,Move,Resize,

Selection



Dispose



Dispose,FocusIn,

FocusOut,Help,

KeyDown,KeyUp,

MouseDoubleClick,

BORDER,SINGLE,

MouseDown,

READ_ONLY,LEFT,CENTER, MouseEnter,MouseExit,

RIGHT,WRAP,MULTI(with MouseHover,MouseUp,

H_SCROLL,V_SCROLL)

MouseMove,Move,

Paint,Resize,

DefaultSelection,Modify,

Verify



Thestandardtoolbara BORDER,FLAT,WRAP,

compositecontrolthat RIGHT,SHADOW_OUT

containstoolbaritems. HORIZONTAL,VERTICAL



Dispose,FocusIn,

FocusOut,Help,

KeyDown,KeyUp,

MouseDoubleClick,

MouseDown,

MouseEnter,MouseExit,

MouseHover,MouseUp,

MouseMove,Move,

Paint,Resize



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

Chapter 7. SWT: Buttons, Text, Labels, Lists, Layouts, and Events

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

×