java布局管理器总结_第1页
java布局管理器总结_第2页
java布局管理器总结_第3页
java布局管理器总结_第4页
java布局管理器总结_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、java布局管理器总结(2013-02-2521:42:32转载标签:转载原文地址:java布局管理器总结作者:技术羊之前在使用的过程中一直对java中swing的布局管理器感到很困惑,以下是在网上找到的一篇文章。其中我重点关注了一下gridbaglayout。写的比较详尽:BorderLayoutFlowLayoutGridLayoutGridBagLayoutCardLayoutBoxLayout1.BorderLayoutjava.lang.Object-java.awt.BorderLayout将版面划分成东、西、南、北、中五个区域,将添加的组件按指定位置放置。BorderLayout

2、.EASTBorderLayout.WESTBorderLayout.SOUTHBorderLayout.NORTHBorderLayout.CENTER构造函数:BorderLayout(建立组件间无间距的BorderLayoutBorderLayout(int hgap,int vgap建立组件间水平间距为hgap,垂直间距为vgap的BorderLayout 例一:javaview plaincopyprint?import java.awt.BorderLayout;import javax.swing.JFrame;import javax.swing.JButton;public

3、class BorderLayoutDemopublic static void main(Stringargs/建立一个JFrame,JFrame的默认LayoutManager为BorderLayout JFrame f=new JFrame(BorderLayout;JButton btn=new JButton(BorderLayout.NORTH;f.add(btn,BorderLayout.NORTH;btn=new JButton(BorderLayout.SOUTH;f.add(btn,BorderLayout.SOUTH;btn=new JButton(BorderLayou

4、t.EAST;f.add(btn,BorderLayout.EAST;btn=new JButton(BorderLayout.West;f.add(btn,BorderLayout.WEST;btn=new JButton(BorderLayout.CENTER;f.add(btn,BorderLayout.CENTER;f.pack(;f.setVisible(true;f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE; 运行结果: 在上例代码的第13,14行之间插入以下代码javaview plaincopyprint?f.setLayout

5、(new BorderLayout(10,10; 运行结果: 2.FlowLayoutjava.lang.Object-java.awt.FlowLayout组件按从左到右而后从上到下的顺序依次排列,一行不能放完则折到下一行。构造函数:FlowLayout(建立一个默认为居中对齐,组件彼此有5单位的水平与垂直间距的FlowLayout FlowLayout(int align建立一个可设置排列方式且组件彼此有5单位的水平与垂直间距的FlowLayout FlowLayout(int align,int hgap,int vgap建立一个可设置排列方式与组件间距的FlowLayout例二:jav

6、aview plaincopyprint?import java.awt.FlowLayout;import javax.swing.JFrame;import javax.swing.JButton;public class FlowLayoutDemopublic static void main(StringargsJFrame f=new JFrame(FlowLayout;f.setLayout(new FlowLayout(;for(int i=0;i7;i+JButton btn=new JButton(Button+i;f.add(btn;f.setSize(300,150;f

7、.setVisible(true;f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE; 运行结果: 3.GridLayoutjava.lang.Object-java.awt.GridLayout矩形网格形式对容器的组件进行布置构造函数:GridLayout(建立一个默认为一行一列的GridLayoutGridLayout(int rows,int cols建立一个指定行(rows和列(cols的GridLayoutGridLayout(int rows,int cols,int hgap,int vgap建立一个指定行(rows和列(cols,且组

8、件间水平间距为hgap、垂直间距为vgap的GridLayout例三:javaview plaincopyprint?import java.awt.GridLayout;import javax.swing.JFrame;import javax.swing.JButton;public class GridLayoutDemopublic static void main(StringargsJFrame f=new JFrame(GridLayout;/设置f的布局管理器为3行3列的GridLayout,组件间水平与垂直间距为5f.setLayout(new GridLayout(3,3

9、,5,5;for(int i=1;i10;i+JButton btn=new JButton(String.valueOf(i;f.add(btn;f.pack(;f.setVisible(true;f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE; 运行结果: 4.GridBagLayoutjava.lang.Object-java.awt.GridBagLayoutGridBagLayout以表格形式布置容器内的组件,将每个组件放置在每个单元格内,而一个单元格可以跨越多个单元格合并成一个单元格,即多个单元格可以组合成一个单元格,从而实现组件的自

10、由布局。构造函数:GridBagLayout(建立一个默认的GridBagLayout每一个单元格都有各自的属性,而这些属性由GridBagConstrainsts类的成员变量来定义,且GridBagConstriaints中的所有成员变量都是public的。java.lang.Object-java.awt.GridBagConstratints构造函数:GridBagConstraints(建立一个默认的GridBagConstraintsGridBagConstraints(intgridx,int gridy,int gridwidth,int gridheight,double we

11、ightx,double weighty,int anchor,int fill,Insets insets,int ipadx,int ipady建立一个指定其参数值的GridBagConstraintsGridBagConstraints的成员变量:int gridxint gridyint gridwidthint gridheightdouble weightxdouble weightyint anchorint fillInsets insetsint ipadxint ipadygridx,gridy:设置组件所处行与列的起始坐标。例如gridx=0,gridy=0表示将组件放置

12、在0行0列单元格内。gridwidth,gridheight:设置组件横向与纵向的单元格跨越个数。可以通过GridBagConstraints的RELETIVE,和REMAINDER来进行指定,它的用法是:当把gridx值设置为GridBagConstriants.RELETIVE时,所添加的组件将被放置在前一个组件的右侧。同理,对gridy值设置为GridBagConstraints.RELETIVE时,所添加的组件将被放置在前一个组件的下方,(这是一种根据前一个组件而决定当前组件的相对放置方式对gridweight和gridheight也可以应用GridBagConstraints的REM

13、AINDER方式,创建的组件会从创建的起点位置开始一直延伸到容器所能允许的界限为止。该功能使得你可以创建跨越某些行或列的组件,从而改变相应方向上组件的数目,即使其后在布局的其他地方添加额外的组件也是如此。weightx,weighty:设置窗口变大时的缩放比例。anchor:设置组件在单元格中的对齐方式。由以下常量来定义GridBagConstraints.CENTERGridBagConstraints.EASTGridBagConstraints.WESTGridBagConstraints.SOUTHGridBagConstraints.NORTHGridBagConstraints.S

14、OUTHEASTGrisBagConstraints.SOUTHWESTGridBagConstraints.NORTHEASTGridBagConstraints.NORTHWESTfill:当某个组件未能填满单元格时,可由此属性设置横向、纵向或双向填满。由以下常量来定义GridBagConstraints.NONEGridBagConstraints.HORIZONTALGridBagConstraints.VERTICALGridBagConstraints.BOTHinsets:设置单元格的间距。java.lang.Object-java.awt.InsetsInsets(int to

15、p,int left,int bottom,int rightipadx,ipady:将单元格内的组件的最小尺寸横向或纵向扩大。若一个组件的尺寸为30*10像素,ipadx=2,ipady=3,则单元格内的组件最小尺寸为34*16像素例四:javaview plaincopyprint?import java.awt.GridBagLayout;import java.awt.GridBagConstraints;import java.awt.Insets;import javax.swing.JFrame;import javax.swing.JButton;public class Gr

16、idBagLayoutDemopublic static void main(StringargsJFrame f=new JFrame(GridBagLayout;f.setLayout(new GridBagLayout(;JButton btn=new JButton(first; GridBagConstraints gbc=new GridBagConstraints(; /设定第一个单元格的属性值gbc.gridx=0;gbc.gridy=0;gbc.gridwidth=1;gbc.gridheight=1;gbc.weightx=0;gbc.weighty=0;gbc.ancho

17、r=GridBagConstraints.NORTHWEST;gbc.fill=GridBagConstraints.NONE;gbc.insets=new Insets(0,0,0,0;gbc.ipadx=0;gbc.ipady=0;f.add(btn,gbc;/设定第二个单元格属性值gbc.gridx=1;gbc.gridy=0;gbc.gridwidth=GridBagConstraints.REMAINDER; gbc.gridheight=1;gbc.weightx=1;gbc.weighty=0;gbc.anchor=GridBagConstraints.CENTER;gbc.fi

18、ll=GridBagConstraints.HORIZONTAL; gbc.insets=new Insets(5,5,5,5;gbc.ipadx=0;gbc.ipady=0;btn=new JButton(second;f.add(btn,gbc;/设定第三个单元格属性值gbc.gridx=0;gbc.gridy=1;gbc.gridwidth=1;gbc.gridheight=GridBagConstraints.REMAINDER;gbc.weightx=0;gbc.weighty=1;gbc.anchor=GridBagConstraints.CENTER;gbc.fill=GridB

19、agConstraints.VERTICAL;gbc.insets=new Insets(0,0,0,0;gbc.ipadx=10;gbc.ipady=10;btn=new JButton(three;f.add(btn,gbc;f.pack(;f.setVisible(true;f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE; 运行结果: 将窗口变大后的效果: 5.CardLayout java.lang.Object-java.awt.CardLayout以层叠的方式布置组件,如同很多张卡片叠在一起,从而只能看到最上面的那一张卡片。构造函数:

20、CardLayout(建立一个无间距的CardLayoutCardLayout(int hgap,int vgap建立一个水平间距为hgap、垂直间距为vgap的CardLayout例五:javaview plaincopyprint?import java.awt.BorderLayout;import java.awt.CardLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.JPanel;imp

21、ort javax.swing.JLabel;import javax.swing.JButton;public class CardLayoutDemoprivate static JPanel p;public static void main(Stringargs JFrame f=new JFrame(CardLayout;p=new JPanel(;/将JPanel p的LayoutManager设置为CardLayout p.setLayout(new CardLayout(;/新建两个JPanelJPanel p1=new JPanel(;JPanel p2=new JPanel

22、(;JLabel lb=new JLabel(first panel;p1.add(lb;lb=new JLabel(second panel;p2.add(lb;/将新建的两个JPanel p1,p2添加到p中p.add(p1,frist;p.add(p2,second;/设置默认显示first所对应的JPanel p1 (CardLayoutp.getLayout(.show(p,frist;JButton cbtn=new JButton(Change;cbtn.addActionListener(new ActionListener(public void actionPerforme

23、d(ActionEvent e/当按下Change按钮时,显示second对应的JPanel p2 (CardLayoutp.getLayout(.show(p,second;f.add(cbtn,BorderLayout.NORTH;f.add(p,BorderLayout.CENTER;f.setSize(400,150;f.setVisible(true;f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE; 运行结果: 按下Change按钮后的结果: 6.BoxLayoutjava.lang.Object-javax.swing.BoxLayo

24、ut以嵌套式盒子来管里容器的布局,通过将组件放入水平或垂直形盒子以多层嵌套的方式进行布局。构造函数:BoxLayout(Container target,int axis建立一个水平或垂直的BoxLayout,BoxLayout提供两个常数X_AXIS和Y_AXIS来表示水平或垂直排列。说到BoxLayout,就不得不提到Box这个Container,Box这个Container默认的Layout为BoxLayout,而它只能使用这个Layout,否则编译时会有Error产生。java.lang.Object-javax.swing.BoxBox有水平的和垂直的两种形式。构造函数:Box(in

25、t axis建立一个Box Container(容器,并指定组件的排列方式,通过使用BoxLayout提供的两个常数X_AXIS和Y_AXIS来指定。方法:public static Box createHorizontalBox(构造一个水平排列的Box组件。javaview plaincopyprint?import javax.swing.Box;import javax.swing.JFrame;import javax.swing.JButton;public class BoxLayoutDemopublic static void main(StringargsJFrame f=new JFrame(BoxLayout;/创建水平Box组件Box box=Box.createHorizontalBox(;JButton btnA=new JButton(A;JButton btnB=new JButton(B;box.add(btnA;box.add(btnB;f.add(box;f.pack(;f.setVisible(true;f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE; 运行结果: public static

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论