资源描述:
《En12图形用户界面基础课件.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Chapter12GUIBasics1MotivationsThedesignoftheAPIforJavaGUIprogrammingisanexcellentexampleofhowtheobject-orientedprincipleisapplied.Inthechaptersthatfollow,youwilllearntheframeworkofJavaGUIAPIandusetheGUIcomponentstodevelopuser-friendlyinterfacesforapplicationsa
2、ndapplets.2ObjectivesTodistinguishbetweenSwingandAWT(§12.2).TodescribetheJavaGUIAPIhierarchy(§12.3).Tocreateuserinterfacesusingframes,panels,andsimpleGUIcomponents(§12.4).Tounderstandtheroleoflayoutmanagers(§12.5).TousetheFlowLayout,GridLayout,andBorderLayoutm
3、anagerstolayoutcomponentsinacontainer(§12.5).TouseJPanelassubcontainers(§12.7).TospecifycolorsandfontsusingtheColorandFontclasses(§§12.7-12.8).Toapplycommonfeaturessuchasborders,tooltips,fonts,andcolorsonSwingcomponents(§12.9).Touseborderstovisuallygroupuser-i
4、nterfacecomponents(§12.9).TocreateimageiconsusingtheImageIconclass(§12.10).3CreatingGUIObjects//CreateabuttonwithtextOKJButtonjbtOK=newJButton("OK");//Createalabelwithtext"Enteryourname:"JLabeljlblName=newJLabel("Enteryourname:");//Createatextfieldwithtext"Typ
5、eNameHere"JTextFieldjtfName=newJTextField("TypeNameHere");//CreateacheckboxwithtextboldJCheckBoxjchkBold=newJCheckBox("Bold");//CreatearadiobuttonwithtextredJRadioButtonjrbRed=newJRadioButton("Red");//Createacomboboxwithchoicesred,green,andblueJComboBoxjcboCol
6、or=newJComboBox(newString[]{"Red","Green","Blue"});ButtonLabelTextfieldCheckBoxRadioButtonComboBox4Swingvs.AWTSowhydotheGUIcomponentclasseshaveaprefixJ?InsteadofJButton,whynotnameitsimplyButton?Infact,thereisaclassalreadynamedButtoninthejava.awtpackage.WhenJav
7、awasintroduced,theGUIclasseswerebundledinalibraryknownastheAbstractWindowsToolkit(AWT).ForeveryplatformonwhichJavaruns,theAWTcomponentsareautomaticallymappedtotheplatform-specificcomponentsthroughtheirrespectiveagents,knownaspeers.AWTisfinefordevelopingsimpleg
8、raphicaluserinterfaces,butnotfordevelopingcomprehensiveGUIprojects.Besides,AWTispronetoplatform-specificbugsbecauseitspeer-basedapproachreliesheavilyontheunderlyingplatform.Withthe