java经典5种 FlowLayout 、BorderLayout、GridLayout、GridBagLayout、CardLayout布局

 

Java 程序通过jvm可以很好的移植到其他平台上,但是java 生成的图形界面样式,在不使用布局的情况下,往往需要重新设定大小,才能在新的平台上调整到最佳样式。这是由于组件的最佳大小 往往是与平台相关的。不同平台上,相同的内容大小可能不一样.java专门提供了LayoutManager(布局管理器)来更好的进行布局管理,使组件的大小和位置调整到最佳。(包括控件无遮挡,无冗余空间)。

(一) 再开始讲解布局之前,这里先普及一下关于布局中的一些常用知识。

1、容器 Container 做过图形界面绘制工作的人基本都知道这个容器的概念。如果第一次接触的话,可以简单的直接把他理解为是一块面板,可以在上面画控件的东西。 常用的Container 包括:window panel scrollPane 其中Window 又包括 Frame(窗体) Dialog(对话框)

2、对容器的布局设置 Container.setLayoutManager(new layoutManager());

 


(二) Java  常用的有 5+1+1种布局。

1)其中5代表 awt布局有5种分别是 FlowLayout 、BorderLayout、GridLayout、GridBagLayout、CardLayout    下边依次来介绍每种布局    

(1)FowLayout 流布局 

  什么是流布局呢,flow 代表流 水流,FlowLayout也就是就是(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )控件就像流水(队列)一样向某个排列,当遇到边界的时候,就折回从下一行继续排队。就像军训的时候人员一排一排的站好,当一排人数满的时候,就另起一行继续排队。

默认情况下,FlowLayout 布局管理器都是从左向右排列所有组件的。

样式如下图

常用的构造函数有三种

FlowLayout()

FlowLayout(int align)

FlowLayout(int align, int hgap, int vgap)

参数解释:

1、其中align 代表的是对齐方式,包括三种,值作为静态常量保存在 FlowLayout中了

FlowLayout.LEFT 从左向右对齐

FlowLayout.RIGHT从右向左对齐

FlowLayout.CENTER从中间向两边对齐

前两种类似于军训的向左看齐、向右看齐,最后一种类似于照相时,以每排中间为基准,两边向中间靠拢。

2、hgap,控件之间的水平间距 ps.注意此处及下文中的间距都是指的任意一个控件与其周边控件控件的水平和垂直的距离

3、vgap,控件之间的垂直间距 

复制代码
 1 import java.awt.FlowLayout;   
 2 import javax.swing.JFrame;   
 3 import javax.swing.JButton;   
 4    
 5    
 6 public class FlowLayoutDemo
 7 {   
 8     public static void main(String[] args)
 9     {   
10         JFrame f=new JFrame("FlowLayout");   
11         f.setLayout(new FlowLayout());   
12         for(int i=0;i<7;i++)
13         {   
14             JButton btn=new JButton("Button"+i);   
15             f.add(btn);   
16         }   
17         f.setSize(300,150);   
18         f.setVisible(true);   
19         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
20     }   
21 }
复制代码

显示效果:


2)BorderLayout  边框布局

Bordern n.边境;边界;国界 BorderLayout 也就是把容器通过边界划分成几个区域,这几个区域各自始终存在, 并且数量始终为5个:东、西、南、北、中(也就是 EAST/WEST/SOUTH/NORTH/CENTER),区域的相对位置如下图.添加的控件可以被指定放置在区域中的任何一个位置。

这个布局有四点需要注意

1、当布局的大小发生调整时 NORTH/SOUTH/CENTER进行水平调整 EAST/WEST/CENTER进行垂直调整 比如说拉宽窗体,那么只有NORTH/SOUTH/CENTER 这三个区域的宽度会变大,WEST和EAST的宽度不发生改变

2、当向BorderLayout布局的容器中添加控件时,需要指定控(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )件要被放置在哪个区域中。否则,系统方法会默认将该控件放置在center区域。

3、每个区域只能添加一个控件或者一个容器。如果反复添加,后续添加的控件会覆盖前边添加的控件。

4、Frame、Dialog、ScrollPane默认使用的都是这个布局,所以直接向这几个容器中添加控件,最终只会显示一个控件

常用的构造函数有两种

BorderLayout()

BorderLayout(int hgap, int vgap)

ps. hgap,区域之间的水平间距。vgap,区域之间的垂直间距。

复制代码
 1 import java.awt.BorderLayout;   
 2 import javax.swing.JFrame;   
 3 import javax.swing.JButton;   
 4    
 5    
 6 public class BorderLayoutDemo 
 7 {   
 8     public static void main(String[] args) 
 9     {   
10         JFrame f=new JFrame("BorderLayout");   
11         JButton btn=new JButton("BorderLayout.NORTH");   
12         f.add(btn,BorderLayout.NORTH); //NORTH、SOUTH、EAST、WEST、CENTER在BorderLayout中被设置为静态变量了
13         btn=new JButton("BorderLayout.SOUTH");   
14         f.add(btn,BorderLayout.SOUTH);   
15         btn=new JButton("BorderLayout.EAST");   
16         f.add(btn,BorderLayout.EAST);   
17         btn=new JButton("BorderLayout.West");   
18         f.add(btn,BorderLayout.WEST);   
19         btn=new JButton("BorderLayout.CENTER");   
20         f.add(btn,BorderLayout.CENTER);   
21         f.pack();   
22         f.setVisible(true);   
23         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
24     }   
25 } 
复制代码

前文中介绍了FlowLayout和BorderLayout 本文我们将会继续介绍java中的布局方式

(3)GridLayout 网格布局

这种布局会将整个容器划分成M行*N列的网格。

如下图:

                                                                                                     
       
       
       

 

 

 

 

由模型图我们可以知道这种布局,类似于我们常见的扫雷、计算器等软件的布局。

 

这种布局的构造函数有三种

 

1 GridLayout()  //一行一列 
2 
3 GridLayout(int rows, int cols) 
4 
5 GridLayout(int rows, int cols, int hgap, int vgap)//hgap 水平间距, vgap垂直间距

 

在向这种容器中添加控件时,会以向左后右,先上后下的顺序添加(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )控件。 而不能在指定的位置中添加控件,换言之控件之间不能留有空白。 下面我们来看代码

复制代码
 1 import java.awt.*;
 2 import javax.swing.*;
 3 
 4 class GridFrame extends JFrame
 5 {
 6     JPanel panel=new JPanel(new GridLayout(4,4,3,3));//构造指定布局的容器
 7     String str[]={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};
 8     public GridFrame(String name)
 9     {
10         super(name); 
11         setLayout(new BorderLayout()); 
12         JButton btnArray[]; 
13         btnArray=new JButton[str.length]; 
14         for(int i=0;i<str.length;i++)
15         {
16             btnArray[i]=new JButton(str[i]);
17             panel.add(btnArray[i]);
18         }
19         setVisible(true);
20         setSize(250,200);
21         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22         setVisible(true);
23     }
24     
25      public static void main(String[] args)
26     {
27         GridFrame gf=new GridFrame("网格布局计算机!");
28     }
29 
30 }  
复制代码

显示效果如下图

 

 

(4)GridBagLayout

GridBagLayout也是一种表格,但是可以通过设定规则更自由的将控件绑定到容器上:

如下图:

 

将容器切割为若干个小的格子Cell,然后向这些Cell中添加控件,同时可以在某一个(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )方向上连续的几个Cell进行拼接,组成一个大的Cell放置控件。

操作步骤如下:

(1)new GridBagLayout() 设置到指定容器上。

(2)GridBagConstraint  gbc 新增这样的一个容器规则。(注意Constraint是规则、约束、条件的意思)

设定的参数规则如下

gridx  gridy 索引

gridwidth gridheight 跨越的索引

fill  是否动态扩充

weightx、weighty 扩大的权重

(3)gb.setConstraints(控件,gbc)  将控件和规则绑定起来

(4)constainer.add(c) 添加容器,然后将控件添加到容器上。

同时gbc 可以重用,反复的设定骨子额,绑定规则,添加容器

重复 2、3、4步

 请参照 如下代码 ,重点注意 addComponent()方法:

复制代码
 1 public class NumberPad {
 2   private static final Insets insets = new Insets(0, 0, 0, 0);
 3   public static void main(final String args[]) {
 4     final JFrame frame = new JFrame("NumberPad");
 5     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口操作
 6     frame.setLayout(new GridBagLayout());//框架布局
 7     JButton button;
 8     //下面利用设立的类对按键进行布局
 9     //第一行
10     button = new JButton("Num");
11     addComponent(frame, button, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
12     button = new JButton("/");
13     addComponent(frame, button, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
14     button = new JButton("*");
15     addComponent(frame, button, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
16     button = new JButton("-");
17     addComponent(frame, button, 3, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
18     //第二行
19     button = new JButton("1");
20     addComponent(frame, button, 0, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
21     button = new JButton("2");
22     addComponent(frame, button, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
23     button = new JButton("3");
24     addComponent(frame, button, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
25     button = new JButton("+");
26     addComponent(frame, button, 3, 1, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
27     // 第三行
28     button = new JButton("4");
29     addComponent(frame, button, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
30     button = new JButton("5");
31     addComponent(frame, button, 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
32     button = new JButton("6");
33     addComponent(frame, button, 2, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
34     //第四行
35     button = new JButton("7");
36     addComponent(frame, button, 0, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
37     button = new JButton("8");
38     addComponent(frame, button, 1, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
39     button = new JButton("9");
40     addComponent(frame, button, 2, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
41     button = new JButton("Enter");
42     addComponent(frame, button, 3, 3, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
43     //第五行
44     button = new JButton("0");
45     addComponent(frame, button, 0, 4, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
46     button = new JButton(".");
47     addComponent(frame, button, 2, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);  
48     frame.setSize(250,250);
49     frame.setVisible(true);
50   }
51   
52     private static void addComponent(Container container, Component component, int gridx, int gridy,
53       int gridwidth, int gridheight, int anchor, int fill) {
54     GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
55         anchor, fill, insets, 0, 0);//建立网格包对象
56     container.add(component, gbc);//添加到容器中
57   }
复制代码

ps 此代码来自http://blog.sina.com.cn/s/blog_6cea57330100pwvq.html

这里有几点要注意的

(1)控制动态扩充的fill参数,有四个值分别为 水平扩充、垂直扩充、水平垂直扩充、不扩充。大家根据自己的实际情况来选择  

(2)weightx、weighty 是扩大的权重,这个权重表示的是,当横向(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )或纵向扩大N的倍数后,那么他占的比重是多少。这里要小心,就是权重大的话,如果往大拖容器size,那么这个控件增长的快,同时当size变小时,权重大的也缩小的更快。所以如果要想让权重大的控件始终大时,需要将几个控件的初始size设定的非常小,这样所有的控件都是在拖大的状态了。

原文地址:https://www.cnblogs.com/ltb6w/p/8409602.html