图形用户界面设计ppt课件.ppt

图形用户界面设计ppt课件.ppt

ID:59327404

大小:100.50 KB

页数:38页

时间:2020-09-20

上传者:U-5097
图形用户界面设计ppt课件.ppt_第1页
图形用户界面设计ppt课件.ppt_第2页
图形用户界面设计ppt课件.ppt_第3页
图形用户界面设计ppt课件.ppt_第4页
图形用户界面设计ppt课件.ppt_第5页
资源描述:

《图形用户界面设计ppt课件.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

图形用户界面设计 BuildingGUIswithAWTTheAWTEventModelTheAWTComponentLibraryJavaFoundationClasses BuildingGUIswithAWT(AbstractWindowToolkit)java.awt包提供了基本的java程序的GUI设计工具。ComponentContainerLayoutManager Component(组件)Java的图形用户界面的最基本组成部分是组件,组件是一个以图形化的方式显示在屏幕上并能与用户进行交互的对象,例如一个按钮,一个标签等。组件不能独立地显示出来,必须将组件放在一定的容器中才可以显示出来。 Container(容器)容器(Container)实际上是Component的子类,因此容器本身也是一个组件,具有组件的所有性质,另外还具有容纳其它组件和容器的功能。 LayoutManager(布局管理器)为了使我们生成的图形用户界面具有良好的平台无关性,Java语言中,提供了布局管理器这个工具来管理组件在容器中的布局,而不使用直接设置组件位置和大小的方式。每个容器都有一个布局管理器,当容器需要对某个组件进行定位或判断其大小尺寸时,就会调用其对应的布局管理器。 在程序中安排组件的位置和大小时,应该注意:容器中的布局管理器负责各个组件的大小和位置,因此用户无法在这种情况下设置组件的这些属性。如果试图使用Java语言提供的setLocation(),setSize(),setBounds()等方法,则都会被布局管理器覆盖。如果用户确实需要亲自设置组件大小或位置,则应取消该容器的布局管理器,方法为:setLayout(null); 常用容器FramePanelApplet Framejava.lang.Object|+----java.awt.Component|+----java.awt.Container|+----java.awt.Window|+----java.awt.Frame importjava.awt.*;publicclassMyFrameextendsFrame{publicstaticvoidmain(Stringargs[]){MyFramefr=newMyFrame("HelloOutThere!");fr.setSize(200,200);fr.setBackground(Color.red);fr.setVisible(true);}publicMyFrame(Stringstr){super(str);}} 运行结果 Paneljava.lang.Object|+----java.awt.Component|+----java.awt.Container|+----java.awt.Panel importjava.awt.*;publicclassFrameWithPanelextendsFrame{publicFrameWithPanel(Stringstr){super(str);}publicstaticvoidmain(Stringargs[]){FrameWithPanelfr=newFrameWithPanel("FramewithPanel");Panelpan=newPanel(); fr.setSize(200,200);fr.setBackground(Color.red);fr.setLayout(null);pan.setSize(100,100);pan.setBackground(Color.yellow);fr.add(pan);fr.setVisible(true);}} 运行结果: LayoutManagerFlowLayoutBorderLayoutGridLayoutCardLayoutGridBagLayoutnull importjava.awt.*;publicclassExGui{privateFramef;privateButtonb1;privateButtonb2;publicstaticvoidmain(Stringargs[]){ExGuithat=newExGui();that.go();} publicvoidgo(){f=newFrame("GUIexample");f.setLayout(newFlowLayout());b1=newButton("PressMe");b2=newButton("Don'tPressMe");f.add(b1);f.add(b2);f.pack();f.setVisible(true);}} 运行结果 FlowLayoutPanel,Applet的缺省布局管理器。setLayout(newFlowLayout(FlowLayout.RIGHT,20,40));setLayout(newFlowLayout(FlowLayout.LEFT));setLayout(newFlowLayout()); importjava.awt.*;publicclassmyButtons{publicstaticvoidmain(Stringargs[]){Framef=newFrame();f.setLayout(newFlowLayout());Buttonbutton1=newButton("Ok");Buttonbutton2=newButton("Open");Buttonbutton3=newButton("Close");f.add(button1);f.add(button2);f.add(button3); f.setSize(300,100);f.setVisible(true);}}运行结果为: BorderLayoutWindow,Frame和Dialog的缺省布局管理器。BorderLayout布局管理器包括5个区域:North,South,East,West和Center。importjava.awt.*;publicclassbuttonDir{publicstaticvoidmain(Stringargs[]){Framef=newFrame("BorderLayout");f.setLayout(newBorderLayout()); f.add("North",newButton("North"));f.add("South",newButton("South"));f.add("East",newButton("East"));f.add("West",newButton("West"));f.add("Center",newButton("Center"));f.setSize(200,200);f.setVisible(true);}} GridLayout使容器中各个组件呈网格状布局。importjava.awt.*;publicclassButtonGrid{publicstaticvoidmain(Stringargs[]){Framef=newFrame("GridLayout");f.setLayout(newGridLayout(3,2));f.add(newButton("1"));f.add(newButton("2")); f.add(newButton("3"));f.add(newButton("4"));f.add(newButton("5"));f.add(newButton("6"));f.setSize(200,200);f.setVisible(true);}} FrameFrame是一个顶级窗口。Frame的缺省布局管理器为BorderLayout。PanelPanel无法单独显示,必须添加到某个容器中。Panel的缺省布局管理器为FlowLayout。当把Panel作为一个组件添加到某个容器中后,该Panel仍然可以有自己的布局管理器。因此,可以利用Panel使得BorderLayout中某个区域显示多个组件。 importjava.awt.*;publicclassExGui3{privateFramef;privatePanelp;privateButtonbw,bc;privateButtonbfile,bhelp;publicstaticvoidmain(Stringargs[]){ExGui3gui=newExGui3();gui.go();} publicvoidgo(){f=newFrame("GUIexample3");bw=newButton("West");bc=newButton("Workspaceregion");f.add(bw,"West");f.add(bc,"Center");p=newPanel();f.add(p,"North");bfile=newButton("File");bhelp=newButton("Help");p.add(bfile);p.add(bhelp);f.pack();f.setVisible(true);}} CardLayoutCardLayout布局管理器能够帮助用户处理两个以至更多的成员共享同一显示空间。CardLayout对象是容器的布局管理器。它将容器中的每个组件当作一个卡片来处理。在某一时间,只有一个卡片是可见的,容器象一个卡片堆栈一样工作。卡片次序是根据容器的组件对象的内部次序来确定的。CardLayout定义了一系列方法,来允许一个应用顺序地翻动这些卡片,或显示一个指定的卡片。 voidaddLayoutComponent(Componentcomp,Objectconstraints)           将指定的组件添加到此卡片布局的内部名称表。voidaddLayoutComponent(Stringname,Componentcomp)已过时。由addLayoutComponent(Component,Object)取代。voidfirst(Containerparent)           翻转到容器的第一张卡片。voidlast(Containerparent)           翻转到容器的最后一张卡片。voidnext(Containerparent)           翻转到指定容器的下一张卡片。voidprevious(Containerparent)           翻转到指定容器的前一张卡片。voidshow(Containerparent,Stringname)           翻转到已添加到此布局(使用addLayoutComponent)的具有指定name的组件。 GridBagLayoutRowsandcolumnscanhavevariablesizes.GridBagConstraintsobjectspecifyhowthecomponentsarelaidoutwithinthegrids.GridBagLayoutlayout=newGridBagLayout();panel.setLayout(layout);GridBagConstraintsc=newGridBagConstraints();c.weightx=100;c.weighty=100;c.gridx=0;c.gridy=2;getContentPane().add(panel,c); GridBagConstraintsgridx,gridy组件在窗格中的位置,即行和列的位置。gridwidth,gridheight指定组件占用的行数和列数增量字段weightx,weighty0表示保持原始大小不变非0(如100)表示大小会扩展到填充满行或列 GridBagConstraintsfill组件在单元格内的填充行为GridBagConstraints.NONEGridBagConstraints.HORIZONTALGridBagConstraints.VERTICALGridBagConstraints.BOTHanchor组件在单元格内的对齐方式GridBagConstraints.CENTERGridBagConstraints.NORTH等 GridBagConstraints组件外部填充量InsetsInsets对象的left,top,right,bottom值计算组件周围的空间量。组件内部填塞量ipadx,ipady被加到组件的最小宽度和最小高度上,保证组件不会收缩到最小尺寸之下。gridx,gridy,gridwidth,gridheight相对位置GridBagConstraints.RELATIVEGridBagConstraints.REMAINDER Null布局管理把布局管理器设为null把组件添加到容器中指定组件需要的位置和大小contentPane.setLayout(null);JButtonok=newJButton(“OK”);contentPane.add(OK);ok.setBounds(10,10,30,15);

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。
关闭