欢迎来到天天文库
浏览记录
ID:41856993
大小:437.00 KB
页数:27页
时间:2019-09-03
《21Java GUI程序设计(一)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、JavaGUI程序设计JavaGUI编程介绍两种主要的容器:Frame/PanelAwt中的布局管理(上)Awt中的布局管理(下)在awt中绘图AWT(AbstractWindowTools)GUI(GraphicsUserInterface):图形用户界面AWT(AbstractWindowTools):抽象窗口工具java.awt包在这个包中,提供了基本的java程序GUI设计工具:Component/MenuComponentContainerLayoutManagerObjectComponentTex
2、tFieldContainerOthers…ButtonContainer(容器)容器(Container)实际上是Component的子类,因此容器类对象本身也是一个组件,具有组件的所有性质,另外还具有容纳其它组件和容器的功能。容器类对象可使用方法add()添加组件两种主要的容器类型Window:可独立存在的顶级窗口Panel:可作为容器容纳其它组件,但不能独立存在,必须被添加到其它容器中(如Window或Applet)Container层次关系图ContainerPanelAppletWindowFrame
3、ScrollPaneDialogContainer常用方法add()setLocation()setSize()setBoundes()setVisible()pack()组件定制组件的大小和位置由布局管理器(LayoutManager)决定。不使用布局管理器则可以定制组件的大小和位置,但必须在容器中使用组件的setLocation(),setSize(),setBounds()方法确定大小位置Frame类是Window类的子类有标题,可通过拖拉改变大小初始化时为不可见,可用setVisible(true)使其
4、显示出来使用BorderLayout作为其缺省布局管理器使用setLayout方法改变布局管理器Frame类例子(示例11-1)importjava.awt.*;publicclassMyFrame{publicstaticvoidmain(Stringargs[]){Framef=newFrame("Hello,MyFrame!");f.setSize(200,200);f.setVisible(true);}}Frame例子(con.)Panel为放置组件提供空间允许使用自己的布局管理器不能单独存在,必须放
5、置到其他容器中Panel例子(示例11-2)importjava.awt.*;publicclassMyPanel{publicstaticvoidmain(Stringargs[]){//definealabelLabell=newLabel("ThisLabel");//defineapanelPanelp=newPanel();//setthepanel'ssizep.setSize(200,100);//addthelabeltothepanelp.add(l);//defineaframeFramef
6、=newFrame("Hello,mypanel!");f.setSize(200,200);//addthepanel"p"totheFrame"f"f.add(p);//packthecomponetstogetherf.pack();f.show();}}Container的布局管理器为了使我们生成的图形用户界面具有良好的平台无关性,Java语言中,提供了布局管理器这个工具来管理组件在容器中的布局,而不使用直接设置组件位置和大小的方式。Container的布局管理器(con.)Awt中的布局管理器有:Fl
7、owLayoutBorderLayoutGridLayoutCardLayoutGridBagLayoutFlowLayoutGUIComponent从左到右按顺序配置在Container中,若到达右边界,则会折回到下一行中FlowLayout是Panel和Applet的默认管理器FlowLayout()/FlowLayout(intalign)/FlowLayout(intalign,inthgap,intvgap)FlowLayout.LEFT/FlowLayout.CENTER/FlowLayout.RI
8、GHT默认为靠中对齐使用组件的理想尺寸FlowLayout例子(示例11-3)importjava.awt.*;publicclassMyFlowLayoutextendsFrame{privateButtonleftButton,centerButton,rightButton;publicMyFlowLayout(){super("MyFlowLayoutTest");//定义按钮
此文档下载收益归作者所有