您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 企业财务 > java语言程序设计-基础篇--第八版--英文课件(第12章)
Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807Chapter12GUIBasics1Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807MotivationsThedesignoftheAPIforJavaGUIprogrammingisanexcellentexampleofhowtheobject-orientedprincipleisapplied.Inthechaptersthatfollow,youwilllearntheframeworkofJavaGUIAPIandusetheGUIcomponentstodevelopuser-friendlyinterfacesforapplicationsandapplets.2Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807ObjectivesTodistinguishbetweenSwingandAWT(§12.2).TodescribetheJavaGUIAPIhierarchy(§12.3).Tocreateuserinterfacesusingframes,panels,andsimpleGUIcomponents(§12.4).Tounderstandtheroleoflayoutmanagers(§12.5).TousetheFlowLayout,GridLayout,andBorderLayoutmanagerstolayoutcomponentsinacontainer(§12.5).TouseJPanelassubcontainers(§12.7).TospecifycolorsandfontsusingtheColorandFontclasses(§§12.7-12.8).Toapplycommonfeaturessuchasborders,tooltips,fonts,andcolorsonSwingcomponents(§12.9).Touseborderstovisuallygroupuser-interfacecomponents(§12.9).TocreateimageiconsusingtheImageIconclass(§12.10).3Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807CreatingGUIObjects//CreateabuttonwithtextOKJButtonjbtOK=newJButton(OK);//CreatealabelwithtextEnteryourname:JLabeljlblName=newJLabel(Enteryourname:);//CreateatextfieldwithtextTypeNameHereJTextFieldjtfName=newJTextField(TypeNameHere);//CreateacheckboxwithtextboldJCheckBoxjchkBold=newJCheckBox(Bold);//CreatearadiobuttonwithtextredJRadioButtonjrbRed=newJRadioButton(Red);//Createacomboboxwithchoicesred,green,andblueJComboBoxjcboColor=newJComboBox(newString[]{Red,Green,Blue});4ButtonLabelTextfieldCheckBoxRadioButtonComboBoxLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807Swingvs.AWTSowhydotheGUIcomponentclasseshaveaprefixJ?InsteadofJButton,whynotnameitsimplyButton?Infact,thereisaclassalreadynamedButtoninthejava.awtpackage.WhenJavawasintroduced,theGUIclasseswerebundledinalibraryknownastheAbstractWindowsToolkit(AWT).ForeveryplatformonwhichJavaruns,theAWTcomponentsareautomaticallymappedtotheplatform-specificcomponentsthroughtheirrespectiveagents,knownaspeers.AWTisfinefordevelopingsimplegraphicaluserinterfaces,butnotfordevelopingcomprehensiveGUIprojects.Besides,AWTispronetoplatform-specificbugsbecauseitspeer-basedapproachreliesheavilyontheunderlyingplatform.WiththereleaseofJava2,theAWTuser-interfacecomponentswerereplacedbyamorerobust,versatile,andflexiblelibraryknownasSwingcomponents.SwingcomponentsarepainteddirectlyoncanvasesusingJavacode,exceptforcomponentsthataresubclassesofjava.awt.Windoworjava.awt.Panel,whichmustbedrawnusingnativeGUIonaspecificplatform.SwingcomponentsarelessdependentonthetargetplatformanduselessofthenativeGUIresource.Forthisreason,Swingcomponentsthatdon’trelyonnativeGUIarereferredtoaslightweightcomponents,andAWTcomponentsarereferredtoasheavyweightcomponents.5Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807GUIClassHierarchy(Swing)6DimensionFontFontMetricsComponentGraphicsObjectColorContainerPanelAppletFrameDialogWindowJComponentJAppletJFrameJDialogSwingComponentsinthejavax.swingpackageLightweightHeavyweightClassesinthejava.awtpackage1LayoutManager*Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807ContainerClasses7DimensionFontFontMetricsComponentGraphicsObjectColorContainerPanelAppletFrameDialogWindowJComponentJAppletJFrameJDialogSwingComponentsinthejavax.swingpackageLightweightHeavyweightClassesinthejava.awtpackage1LayoutManager*JPanelContainerclassescancontainotherGUIcomponents.Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807GUIHelperClasses8DimensionFontFontMetricsComponentGraphicsObjectColorContainerPanelAppletFrameDialogWindowJComponentJAppletJFrameJDialogSwingComponentsinthejavax.swingpackageLightweightHeavyweightClassesinthejava.awtpackage1LayoutManager*JPanelThehelperclassesarenotsubclassesofComponent.TheyareusedtodescribethepropertiesofGUIcomponentssuchasgraphicscontext,colors,fonts,anddimension.Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807SwingGUIComponents9JMenuItemJCheckBoxMenuItemAbstractButtonJComponentJMenuJRadioButtonMenuItemJToggleButtonJCheckBoxJRadioButtonJComboBoxJInternalFrameJLayeredPaneJListJMenuBarJOptionPaneJPopupMenuJProgressBarJFileChooserJScrollBarJScrollPaneJSeparatorJSplitPaneJSliderJTabbedPaneJTableJTableHeaderJTextFieldJTextComponentJTextAreaJToolBarJToolTipJTreeJRootPaneJPanelJPasswordFieldJColorChooserJLabelJEditorPaneJSpinnerJButtonLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807ComponentsCoveredintheBriefVersion10JMenuItemJCheckBoxMenuItemAbstractButtonJC
本文标题:java语言程序设计-基础篇--第八版--英文课件(第12章)
链接地址:https://www.777doc.com/doc-2881765 .html