介绍-AWT:使用窗口、图形、文本

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

21AWT19AWTAWTAWTAWTAWTAWT22AWTJava23AWTAWTGUIJava20,21.1AWTAWTjava.awtJava21-1AWT21-1AWTClassAWTEventAWTAWTEventMulticasterBorderLayoutNorthSouthEastWestCenterButtonCanvasCardLayoutCheckboxCheckboxGroupCheckboxMenuItem/21AWT471ClassChoiceColorComponentAWTContainerCursorDialogDimensionwidthheightEventEventQueueFileDialogFlowLayoutFontFontMetricsFrameGraphicsGraphicsDeviceGraphicsEnvironmentFontGraphicsDeviceGridBagConstraintsGridBagLayoutGridBagLayoutGridBagConstraintsGridLayoutImageInsetsLabelListMediaTrackerMenuMenuBarMenuComponentMenuItemMenuShortcutPanelPointxyPolygonPopupMenuPrintJob4722JavaClassRectangleRobotAWTJava2,v1.3ScrollbarsScrollPane/SystemColorGUITextAreaTextComponentTextAreaTextFieldTextFieldToolkitAWTWindowJava1.0AWTJava1.1Java221.2AWTPanelFramePanelFrame21-1PanelFrame21.2.1ComponentAWTComponentComponentComponent(1920)Component21AWT47321-1PanelFrame21.2.2ContainerContainerComponentComponentContainerContainerContainerComponent2221.2.3PanelPanelContainerContainerPanelPanelAppletPanelPanelPaneladd()PanelContainerComponentsetLocation(),setSize()setBounds()21.2.4WindowWindowWindowWindowFrame21.2.5FrameFrameWindowFrameComponentContainerPanelWindowMenuContainerInterfaceFrame4742JavaJavaAppletWindowFrame21.2.6CanvasframeCanvasCanvasCanvas21.3FrameFrameFrameFrame()Frame(Stringtitle)titleFrame21.3.1setSize()voidsetSize(intnewWidth,intnewHeight)voidsetSize(DimensionnewSize)newWidthnewHeightDimensionnewSizewidthheightgetSize()DimensiongetSize()Dimensionwidthheight21.3.2framesetVisible()21AWT475voidsetVisible(booleanvisibleFlag)true21.3.3setTitle()framevoidsetTitle(StringnewTitle)newtitle21.3.4frameframesetVisible(false)WindowListenerwindowClosing()windowClosing()21.4frameframeframeframeframeframeinit()start()stop()paint()windowListenerwindowClosing()setVisible(false)frameframesetVisible()setSize()SampleFrameFrameAppletFrameinit()SampleFrameFrameframetitlestart()stop()//Createachildframewindowfromwithinanapplet.importjava.awt.*;4762Javaimportjava.awt.event.*;importjava.applet.*;/*appletcode=AppletFramewidth=300height=50/applet*///CreateasubclassofFrame.classSampleFrameextendsFrame{SampleFrame(Stringtitle){super(title);//createanobjecttohandlewindoweventsMyWindowAdapteradapter=newMyWindowAdapter(this);//registerittoreceivethoseeventsaddWindowListener(adapter);}publicvoidpaint(Graphicsg){g.drawString(Thisisinframewindow,10,40);}}classMyWindowAdapterextendsWindowAdapter{SampleFramesampleFrame;publicMyWindowAdapter(SampleFramesampleFrame){this.sampleFrame=sampleFrame;}publicvoidwindowClosing(WindowEventwe){sampleFrame.setVisible(false);}}//Createframewindow.publicclassAppletFrameextendsApplet{Framef;publicvoidinit(){f=newSampleFrame(AFrameWindow);f.setSize(250,250);f.setVisible(true);}publicvoidstart(){f.setVisible(true);}publicvoidstop(){f.setVisible(false);}publicvoidpaint(Graphicsg){g.drawString(Thisisinappletwindow,10,20);}}21AWT47721.4.1FrameFrameComponentComponentframepaint()repaint()//Handlemouseeventsinbothchildandappletwindows.importjava.awt.*;importjava.awt.event.*;importjava.applet.*;/*appletcode=WindowEventswidth=300height=50/applet*///CreateasubclassofFrame.classSampleFrameextendsFrameimplementsMouseListener,MouseMotionListener{Stringmsg=;intmouseX=10,mouseY=40;intmovX=0,movY=0;SampleFrame(Stringtitle){super(title);//registerthisobjecttoreceiveitsownmouseeventsaddMouseListener(this);addMouseMotionListener(this);//createanobjecttohandlewindoweventsMyWindowAdapteradapter=newMyWindowAdapter(this);//registerittoreceivethoseeventsaddWindowListener(adapter);}//Handlemouseclicked.4782JavapublicvoidmouseClicked(MouseEventme){}//Handlemouseentered.publicvoidmouseEntered(MouseEventevtObj){//savecoordinatesmouseX=10;mouseY=54;msg=Mousejustenteredchild.;repaint();}//Handlemouseexited.publicvoidmouseExited(MouseEventevtObj){//savecoordinatesmouseX=10;mouseY=54;msg=Mousejustleftchildwindow.;repaint();}//Handlemousepressed.publicvoidmousePressed(MouseEventme){//savecoordinatesmouseX=me.getX();mouseY=me.getY();msg=Down;repaint();}//Handlemousereleased.publicvoidmouseReleased(MouseEventme){//savecoordinatesmouseX=me.getX();mouseY=me.getY();msg=Up;repaint();}//Handlemousedragged.publicvoidmouseDragged(MouseEventme){//savecoordinatesmouseX=me.getX();mouseY=me.getY();movX=me.getX();movY=me.getY();msg=*;repaint();}//Handlemousemoved.publicvoidmouseMoved(MouseEventme){//savecoordinatesmovX=me.getX();movY=me.getY();repaint(0,0,100,60);21AWT479}publicvoidpaint(Graphicsg){g.drawString(msg,mouseX,mouseY);g.drawString(Mouseat+movX+,+movY,10,40);}}classMyWindowAdapterextendsWindowAdapter{SampleFramesampleFrame;publicMyWindowAdapter(SampleFramesampleFrame){this.sampleFrame=sampleFrame;}publicvoidwindowClosing(WindowEventwe){sampleFrame.setVisible(false);}}//Appletwindow.publicclassWindowEventsextendsAppletimplementsMouseListener,MouseMotionListener{SampleFramef;Stringmsg=;intmouseX=0,mouseY=10;intmovX=0,movY=0;//Createaframewindow.publicvoidinit(){f=newSampleFrame(HandleMouseEvents);f.setSize(300,200);f.setVisible(t

1 / 36
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功