您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > Java第06章--2(布局与事件处理)
JavaJavapenggang@mail.hust.edu.cn12AWT(AbstractWindowToolkit)3Swing4566.44Java(LayoutManager)4setLocation(),setSize(),setBounds()setLayout(null)setLocation(),setSize(),setBounds()(“”)4java.awtFlowLayoutPanelAppletBorderLayoutWindowDialogFrameGridLayoutCardLayoutGridBagLayout•1•2ContainersetLayout()•3Containeradd()•SwingAbstractButtonaddActionListener()–6.7,P365:FlowLayoutDemo.java4——BorderLayoutBorderLayout5North,South,East,WestCenterBorderLayoutpublicBorderLayout()0publicBorderLayout(inthorizontalGap,intverticalGap)NorthSouthWestEastCenter6.8,P370:BorderLayoutDemo.java•–(1)–(2)4–(3)6.31•.4——GridLayoutGridLayoutBorderLayoutPanelPanel.GridLayoutpublicGridLayout()0publicGridLayout(introws,intcols)rowscolsrowscols0rows0colscols0rows0publicGridLayout(introws,intcols,inthorizontalGap,intverticalGap)6.9,P373:GridLayoutDemo.java4——CardLayoutCardLayoutCardLayoutpublicvoidshow(Containerparent,Stringname)publicvoidnext(Containerparent)publicvoidprevious(Containerparent)publicvoidfirst(Containerparent)publicvoidlast(Containerparent)ContainerCardLayout4——CardLayoutCardLayoutpublicCardLayout()0publicCardLayout(inthorizontalGap,intverticalGap)BorderLayoutGridLayoutCardLayoutadd(String,Component);4——GridBagLayoutGridBagLayoutAWTGridLayoutGridBagLayoutGridBagConstraints()GridBagLayout,.GridBagLayoutmyLayout=newGridBagLayout();4——GridBagLayoutGridBagConstraintsgridx,gridygridwidth,gridheightfillipadx,ipadyinsetsarchorweightx,weighty4——GridBagLayoutGridBagLayoutGridBagConstraints//GridBagLayoutg=newGridBagLayout();GridBagConstraintsc=newGridBagConstraints();setLayout(g);Buttonb=newButton(“Test”);//bc.fill=…//c……g.setConstraints(b,c);/*cb*/add(b);4——GridBagLayoutGridBagLayoutGridBagConstraintsGridBagConstraintsGridBagWindow.java6.55JavaclassActionEventWindowEventJDK1.01.125——JDK1.0JDK1.0java.awt.Eventjava.lang.ObjectEventidif(Ev.id==ACTION_EVENT){…}5——JDK1.0JDK1.0“”HierarchicaleventmodelAWTComponentaction(Evente,Objectarg)handleEvent(Evente)AWTJDK1.0JDK1.0action()handleEvent()AWTactionJDK1.05—JDK1.1JDK1.1java.awt.eventAWTEventAWTEventAWTAWTjava.util.EventObjectjava.awt.event.AWTEventAWTEventAWTEventComponentEvent,ContainerEvent,WindowEvent,FocusEvent,KeyEvent,MouseEventHierarchyEvent,InputMethodEvent,InvocationEvent,GUIActionEvent,AdjustmentEvent,ItemEvent,TextEvent•high-leveleventlow-leveleventsemanticevent•“”••AWTactionPerformed()JDK1.1[listener]“-”DelegationeventmodelActionEventWindowEvent“”“”ComponentaddXXXListener(ListenerTypelistener)ActionPerformed(ActionEvente)removeXXXListener(ListenerTypelistener)TestExitActionEventactionPerformed(ActionEvente)FrameButtonListenerXXXEventXXXListeneraddXXXListener(XXXListener)6.11P387ButtonEventTest.java•AWTSwingActionListener()•“”ActionaddActionListener()“”publicvoidaddActionListener(ActionListenerl){...}ActionListenerl“”“”“”.addActionListener();•ActionListenerEventListeneractionPerformed()publicinterfaceActionListenerextendsEventListener{publicvoidactionPerformed(ActionEvente);}ActionEventactionPerformed()••XXXListenerJDK1.1XXXAdapter6.66——……AWTGUIWindow.javaSwingGUIWindow2.javanewsetLayoutaddaddListenerj2sdk
本文标题:Java第06章--2(布局与事件处理)
链接地址:https://www.777doc.com/doc-6147836 .html