1 / 30

第八章 面板与布局管理器 8 . 1 布局管理器 AWT 面板可以包含组件和其它面板。面板把对组件的布置 授权 给一个布局管理器。 由 java 支持的基本布局管理器有:

第八章 面板与布局管理器 8 . 1 布局管理器 AWT 面板可以包含组件和其它面板。面板把对组件的布置 授权 给一个布局管理器。 由 java 支持的基本布局管理器有: FlowLayout GridLayout BorderLayout CardLayout. 1 、 FlowLayout (流布局管理器) 使用这种布局管理器,其 布局策略是容器中的组件按照加入的先后顺序从左到右排列。 当一行排满后会转到下一行显示,每一行中的组件都居中排列。 流布局管理器还有个 对齐参数 ,决定着每行的对齐: FlowLayout.LEFT

iden
Download Presentation

第八章 面板与布局管理器 8 . 1 布局管理器 AWT 面板可以包含组件和其它面板。面板把对组件的布置 授权 给一个布局管理器。 由 java 支持的基本布局管理器有:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第八章 面板与布局管理器 8 . 1布局管理器 AWT面板可以包含组件和其它面板。面板把对组件的布置授权给一个布局管理器。 由java支持的基本布局管理器有: FlowLayout GridLayout BorderLayout CardLayout

  2. 1、FlowLayout(流布局管理器) 使用这种布局管理器,其布局策略是容器中的组件按照加入的先后顺序从左到右排列。 当一行排满后会转到下一行显示,每一行中的组件都居中排列。 流布局管理器还有个对齐参数,决定着每行的对齐: FlowLayout.LEFT FlowLayout.RIGHT FlowLayout.CENTER 缺省为居中对齐

  3. FlowLayout 是Panel和applet的默认布局管理器。 FlowLayout类有下列构造方法: public FlowLayout() public FlowLayout(int align) public FlowLayout(int align,int hgap, int vgap) public FlowLayout()构造方法创建一个新的FlowLayout管理器对象以中心对齐并留以缺省的5个像素的水平和垂直间距。 public FlowLayout(int align)构造方法以指定的定位方式、缺省的水平和垂直间距创建一个新的FlowLayout管理器。

  4. 定位变元必须是 FlowLayout.LEFT FlowLayout.RIGHT 或 FlowLayout.CENTER 三者之一。 public FlowLayout(int align,int hgap,int vgap)构造方法以指定的定位方式和指定的水平和垂直间距创建一个新的FlowLayout管理器对象。 align - 是定位值 hgap - 构件之间的水平间距 vgap - 构件之间的垂直间距

  5. 下列代码创建FlowLayout管理器对象并在容器里放置三个按钮。下列代码创建FlowLayout管理器对象并在容器里放置三个按钮。 使用流布局管理器局部代码 Button button1,button2,button3; FlowLayout flow; flow = new FlowLayout(FlowLayout.LEFT,10,10); setLayout(flow); button1=new Button("Button 1"); button2=new Button("Button 2"); button3=new Button("Button 3"); add(buttonl); add(button2); add(button3);

  6. 2、GridLayout(网格布局管理器) 使用这种布局管理器,面板的部分空区域变成行列,把添加到面板的每个部件放入网格的一个单元,从顶行开始,每行从左到右进行布局。(调用add()方法的顺序非常重要) GridLayout类有下列构造方法: public GridLayout(int rows,int cols) public GridLayout(int rows,int cols,int hgap,int vgap)

  7. public GridLayout(int rows,int cols)构造方法创建一个带指定行数和列数的格布局。 在布局中所有构件有同样的尺寸。 如果rows或者cols中有一个为零,就意味着在一行或一列中可以放置任何数目的对象。 public GridLayout(int rows,int cols,int hgap,int vgap)构造方法是一个带指定行数和列数的格子布局,在布局中所有构件有同样的尺寸, rows或者cols为零意味着“任意数目”。 另外,水平和垂直间距设置为指定值。 水平间距放置在每个列之间的左、右边处; 垂直间距放置在每个行之间的顶、底边处。

  8. 使用网格布局管理器局部代码 Button button1,button2,button3,button4; GridLayout g1=new GridLayout(2,2); setLayout(g1); button1=new Button("Button 1"); button2=new Button("Button 2"); button3=new Button("Button 3"); button4=new Button("Button 4"); add(buttonl); add(button2); add(button3); add(button4);

  9. 注意: GridLayout类用于在所有构件有相同尺寸的格子里展示Container对象的构件。这种布局以指定行数和列数规定构件。 3、BorderLayout(边界布局管理器) 使用这种布局管理器,当添加一个部件到面板时需要指明其地理方位(South、East、North、Center、West),它也可以有水平和垂直间隙。 它是windows、Dialog和Frame容器的默认布局管理器。

  10. BorderLayout类有下列构造方法: public BorderLayout() public BorderLayout(int hgap,int vgap) public BorderLayout()构造方法创建新的边界布局。 public BorderLayout(int hgap,int int vgap) 构造方法以指定的水平和垂直间距创建新的边界布局。水平和垂直间距规定构件之间的空格。 hgap-水平间距; vgap-垂直间距 下列程序使用了BorderLayout管理器对象并在applet中放置了五个按钮。

  11. 使用边界布局管理器局部代码 Button button1,button2,button3,button4,button5; BorderLayout b1; b1=new BorderLayout(); setLayout(b1); button1=new Button("Button 1"); button2=new Button("Button 2"); button3=new Button("Button 3"); button4=new Button("Button 4"); button5=new Button("Button 5"); add("North",button1); add("South",button2);

  12. add("East",button3); add("West",button4); add("Center",button5); 4、CardLayout(卡片布局管理器) 使用这种布局管理器,一般用于组织多个面板以便它们能像一堆卡片那样工作。 基本上是有一组面板被添加到一个卡片布局中,但一次只有一个是可见的。 卡片布局管理器用于部件的滑动显示。 可以通过卡片进行调遣,就像观看幻灯片一样。

  13. 每个面板卡片可使用自己的布局管理程序组织它所包每个面板卡片可使用自己的布局管理程序组织它所包 含的组件。 因为每个卡片使用不同的布局管理器。因此,每个面 板屏幕都有它自己的形状。 CardLayout共提供下列构造方法: public CardLayout() Public CardLayout(int hgap,int vgap)

  14. public CardLayout()构造方法创建一个新的卡片布局 public CardLayout(int hgap,int vgap)构造方法创建一个带指定水平和垂直间距的新的卡片布局。 水平间距安排在左边和右边。 垂直间距被放置在顶边和底边处。 hgap - 水平间距 vgag - 垂直间距 为在布局盒中卡片之间的切换,CardLayout类提供下列方法。

  15. 注意: CardLayout类用于以一副卡片的形式展示Container对象的构件,每次只能看见一张卡片。这类用于规定在容器里构件的顺序(第一、最后、下一个和前一个)。 使用卡片布局管理器局部代码 Button button1,button2,button3; CardLayout c1; Panel p1=new Panel(); add(p1); //将p1添加到applet中

  16. c1=new CardLayout(); p1.setLayout(c1); //设置p1的布局管理器 buttonl=new Button("Button 1"); button2=new Button("Button 2"); button3=new Button("Button 3"); buttonl.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); p1.add(“Button 1",button1); p1.add(“Button 2",button2); p1.add(“Button 3",button3);

  17. 在上述程序中的每个卡片都有一个按钮。 当执行这代码时可在显示中看见单个按钮。 单击按钮可以切换到盒中的下一个按钮。 在到达按钮3并单击它就返回到按钮1。 为时常能翻转按钮,必须使用actionPerformed()方法来处理按钮点击事件。 public void actionPerformed(ActionEvent e) { c1.next(p1); //c1是CardLayout,p1是applet 中的panel }

  18. 5、BoxLayout布局 javax.swing 包提供了一个Box类,它创建的容器称 为盒式容器。其默认的布局管理器是BoxLayout,并且 不能改变盒式容器的布局。 这样的容器将组件排列在一行或一列。 构造方法: BoxLayout(Container con, int axis) 使用BoxLayout的容器 BoxLayout.X_AXIS或BoxLayout.Y_AXIS

  19. 类方法: createHorizontalBox():获得一个具有行型盒式布局的盒式容器。 createVerticalBox():获得一个具有列型盒式布局的盒式容器。 例子:P142/12.5 关键代码: Box boxH=Box.createHorizontalBox();

  20. 支撑:盒式容器中组件间的距离。 Box.createHorizontalStrut(int width): 创建不可见的水平Struct类型的对象(水平支撑)。 高度为0,宽度为width Box.createVerticalStrut(int height): 创建不可见的垂直Struct类型的对象(垂直支撑)。 宽度为0,高度为width

  21. 胶水:胶水组件可处理盒式布局容器的剩余空间。胶水:胶水组件可处理盒式布局容器的剩余空间。 Box.createHorizontalGlue(): 创建不可见的水平Glue类型的对象(水平胶水)。 它会帮助组件靠左/右对齐。P143 Box.createVerticalGlue(): 创建不可见的垂直Struct类型的对象(垂直胶水)。 它会帮助组件靠上/下对齐。P144

  22. 支撑:盒式容器中组件间的距离。 胶水:胶水组件可处理盒式布局容器的剩余空间。 Box.createHorizontalStrut(int width): 创建不可见的水平Struct类型的对象(水平支撑)。 高度为0,宽度为width Box.createHorizontalGlue(): 创建不可见的水平Glue类型的对象(水平胶水)。 它会帮助组件靠左/右对齐。P143 Box.createVerticalStrut(int height): 创建不可见的垂直Struct类型的对象(垂直支撑)。 宽度为0,高度为width Box.createVerticalGlue(): 创建不可见的垂直Struct类型的对象(垂直胶水)。 它会帮助组件靠上/下对齐。P144

  23. 6、Null和自定义布局管理器 当设置布局管理器为null(空布局)时,需要通过调用各组件的方法setBounds()来安排每个组件的在容器中的位置和本身的大小。 setBounds()的定义: void setBounds(int x, int y, int width, int height); 其中的参数给出组件所占据的矩形区域。 向空布局的容器添加一个组件需要两步: 1、用add方法向容器添加组件 2、组件再调用setBounds方法设置该组件在容器中的位置和本身的大小。

  24. 8 . 2 为面板布局 在applet中能够包含组件是因为它是 Panel 面板的子类 ,Panel的子类applet包含AWT组件。 对一个容器的布局/安排通常是先创建一个布局管理器,然后再设置它。 1、用new 方法创建一个布局管理器对象 2、用面板的setLayout()方法来授权给这个布局管理器对该面板进行布局。 3、然后在init()方法中添加组件等。

  25. 示例 import java.awt.*; import java.applet.*; public class AppletWithLayout extends Applet { Button ok=new Button(“确定”); Button cancel=new Button(“取消”); Button apply=new Button(“应用”); Button option=new Button(“选项”); Button properties=new Button(“属性”);

  26. Button ok1=new Button(“确定”); Button cancel1=new Button(“取消”); Button apply1=new Button(“应用”); Button option1=new Button(“选项”); Button properties1=new Button(“属性”); Button ok2=new Button(“确定”); Button cancel2=new Button(“取消”); Button apply2=new Button(“应用”); Button option2=new Button(“选项”); Button properties2=new Button(“属性”);

  27. public void init() { Panel flowPanel=new Panel(); FlowLayout flowbuttons= new FlowLayout (FlowLayout.CENTER, 10,15); flowPanel.setLayout (flowbuttons); flowPanel.add(ok); flowPanel.add(cancel); flowPanel.add(apply); flowPanel.add(option); flowPanel.add(properties);

  28. Panel gridPanel=new Panel(); GridLayout gridbuttons=new GridLayout(0,3,10,15); gridPanel.setLayout (gridbuttons); gridPanel.add(ok1); gridPanel.add(cancel1); gridPanel.add(apply1); gridPanel.add(option1); gridPanel.add(properties1); Panel borderPanel=new Panel(); BorderLayout borderbuttons=new BorderLayout(10,15); borderPanel.setLayout (borderbuttons); borderPanel.add(“North”,ok2);

  29. borderPanel.add(“East”,cancel2); borderPanel.add(“West”,apply2); borderPanel.add(“South”,option2); borderPanel.add(“Center”,properties2); CardLayout appletLayout=new CardLayout (); setLayout(appletLayout); //为applet设置布局管理器 add(flowPanel); add(gridPanel); add(borderPanel); flowPanel.show(false); gridPanel .show(true); borderPanel .show(false); } }

More Related