
21JavaGUI程序设计一.ppt
27页Java GUI程序设计•Java GUI编程介绍•两种主要的容器:Frame/Panel•Awt中的布局管理(上)•Awt中的布局管理(下)•在awt中绘图AWT (Abstract Window Tools)•GUI(Graphics User Interface):图形用户界面•AWT (Abstract Window Tools):抽象窗口工具java.awt包•在这个包中,提供了基本的java程序GUI设计工具:–Component/MenuComponent–Container–LayoutManagerObjectComponentTextFieldContainerOthers…ButtonContainer(容器)•容器(Container)实际上是Component的子类,因此容器类对象本身也是一个组件,具有组件的所有性质,另外还具有容纳其它组件和容器的功能•容器类对象可使用方法add()添加组件•两种主要的容器类型–Window:可独立存在的顶级窗口–Panel:可作为容器容纳其它组件,但不能独立存在,必须被添加到其它容器中(如Window 或 Applet)Container层次关系图ContainerPanelAppletWindowFrameScrollPaneDialogContainer常用方法•add()•setLocation()•setSize()•setBoundes()•setVisible()•pack()组件定制•组件的大小和位置由布局管理器(LayoutManager)决定。
•不使用布局管理器则可以定制组件的大小和位置,但必须在容器中使用组件的setLocation(), setSize(), setBounds()方法确定大小位置Frame类•是Window类的子类•有标题,可通过拖拉改变大小•初始化时为不可见,可用setVisible(true)使其显示出来•使用BorderLayout作为其缺省布局管理器•使用setLayout方法改变布局管理器Frame类例子(示例11-1)import java.awt.*;public class MyFrame{public static void main(String args[]){Frame f=new Frame("Hello,My Frame!");f.setSize(200,200);f.setVisible(true);}}Frame例子(con.)Panel•为放置组件提供空间•允许使用自己的布局管理器•不能单独存在,必须放置到其他容器中Panel例子(示例11-2)import java.awt.*;public class MyPanel{public static void main(String args[]){//define a labelLabel l=new Label("This Label");//define a panelPanel p=new Panel();//set the panel's sizep.setSize(200,100);//add the label to the panelp.add(l);//define a frameFrame f=new Frame("Hello,my panel!");f.setSize(200,200);//add the panel "p" to the Frame "f"f.add(p);//pack the componets togetherf.pack();f.show();}}Container的布局管理器•为了使我们生成的图形用户界面具有良好的平台无关性,Java语言中,提供了布局管理器这个工具来管理组件在容器中的布局,而不使用直接设置组件位置和大小的方式。
Container的布局管理器(con.)•Awt中的布局管理器有:–FlowLayout–BorderLayout–GridLayout–CardLayout–GridBagLayoutFlowLayout•GUI Component从左到右按顺序配置在Container中,若到达右边界,则会折回到下一行中•FlowLayout是Panel和Applet的默认管理器•FlowLayout()/ FlowLayout(int align)/ FlowLayout(int align,int hgap,int vgap)•FlowLayout.LEFT/ FlowLayout.CENTER/ FlowLayout.RIGHT•默认为靠中对齐•使用组件的理想尺寸FlowLayout 例子(示例11-3)import java.awt.*;public class MyFlowLayout extends Frame{private Button leftButton,centerButton,rightButton;public MyFlowLayout(){super("My FlowLayout Test");//定义按钮leftButton=new Button("左");centerButton=new Button("中");rightButton=new Button("右");//居中对齐setLayout(new FlowLayout(FlowLayout.CENTER));//加上按钮add(leftButton);add(centerButton);add(rightButton);//设置大小setSize(200,100);//显示show();}public static void main(String args[]){MyFlowLayout app=new MyFlowLayout();}}BorderLayout•BorderLayout将Container分为EAST、SOUTH、WEST、NORTH、CENTER五个区域,Component可以放置在这五个区域的任何一个•BorderLayout是Frame、Dialog的默认管理器•如果在一个区域中放入多个Component,后放入的Component会把前面的覆盖•BorderLayout()/ BorderLayout(int hgap,int vgap)• BorderLayout.EAST、BorderLayout.SOUTH、BorderLayout.WEST、BorderLayout.SOUTH、BorderLayout.CENTERBorderLayout(con.)•BorderLayout布局格式:当改变容器大小时–North, South和Center区域水平调整–East, West和Center区域垂直调整North South West East CenterBorderLayout例子(示例11-4)import java.awt.*;public class MyBorderLayout extends Frame{private String name[]={"东","南","西","北","中"};private Button button[]=new Button[name.length];public MyBorderLayout(){super("My BorderLayout Test");for (int i=0;i












