您的位置:首页 > 编程语言 > Java开发

Swing学习----------java的布局管理学习总结(一)

2017-05-12 21:20 501 查看
最近java学习了击中布局管理,现来依此总结其各自特点:

  1.FlowLayout布局管理:

      a.FlowLayout布局规律:从上到下,从左到右,根据框架的长和宽,组件从第一行依此向右排列,如果长度不够则自动排列到下一行;

      b.如果在程序中使用了FLowLayout布局,就不能再设置组件的位置

      c.使用无参的FlowLayout()方法添加组件,默认是居中排列的,我们可以看到JAVA源码中是这样定义无参的FlowLayout方法的:

      d.Panel类的默认布局是使用FlowLayout

 


           后面添加的组件是每次把上一次添加的往左边挤,可以通过设置FlowLayout的参数来确定排列方法,整个流布局的示例代码如下:

   

public test(){
/**
* BorderLayout布局学习
*/

this.setTitle("布局管理器的学习");
this.setSize(300, 400);
this.setLocation(400, 300);

this.setLayout(new FlowLayout());

JButton btn1=new JButton("button1");
JButton btn2=new JButton("button2");
JButton btn3=new JButton("button3");
JButton btn4=new JButton("button4");
JButton btn5=new JButton("button5");

this.add(btn1);
this.add(btn2);
this.add(btn3);
this.add(btn4);
this.add(btn5);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

public static void main(String[] args) {
new test();
}


测试结果如下:


                                             


   2.BorderLayout布局管理

       a.在JFrame中,默认布局是使用BorderLayout

       b.BorderLayout布局管理器将容器划分为东南西北中五个区域,如果在使用add方法添加组件时没有指定插入的位置,则默认把所有组件都放置到中部着一个位置,并且后放的组件会覆盖之前的组件,所以最终看到的是最后放置的组件,如下图所示:



public void testBorderLayout(){
this.setTitle("布局管理器的学习");
this.setSize(300, 400);
this.setLocation(400, 300);

this.setLayout(new BorderLayout());

JButton btn1=new JButton("button1");
JButton btn2=new JButton("button2");
JButton btn3=new JButton("button3");
JButton btn4=new JButton("button4");
JButton btn5=new JButton("button5");

this.add(btn1);
this.add(btn2);
this.add(btn3);
this.add(btn4);
this.add(btn5);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
        c.如果改变容器的大小,四边的组件的长度或者宽度不变,中间组件的长度和宽度随着容器大小变化,演示如图:

public void testBorderLayout(){
this.setTitle("布局管理器的学习");
this.setSize(300, 200);
this.setLocation(400, 300);

this.setLayout(new BorderLayout());

JButton btn1=new JButton("button1");
JButton btn2=new JButton("button2");
JButton btn3=new JButton("button3");
JButton btn4=new JButton("button4");
JButton btn5=new JButton("button5");

this.add(btn1,"North");
this.add(btn2,"West");
this.add(btn3,"East");
this.add(btn4,"South");
this.add(btn5,"Center");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}



                     

                            

      d.如果东南西北某个方向不添加组件,将会由其他三个相连的组件占据,但是如果中部不添加组件,会把中部空出来,演示如图:


            


                    

    3.GridLayout布局管理

           a.GridLayout将容器划分为大小相等的若干行列的网格,组件大小随着容器的大小变化而变化,演示及代码如下:

public void testGridLayout(){
this.setTitle("布局管理器的学习");
this.setSize(300, 200);
this.setLocation(400, 300);

this.setLayout(new GridLayout(3,3));

JButton[] btn=new JButton[9];

for(int i=0;i<btn.length;i++){
btn[i]=new JButton(i+"");
this.add(btn[i]);
}

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}


 

                                                     


           b.GridLayout的排列规则是行优先,例如一个3x3的网格,首先第1个组件填充第1行,第2个组件填充第2行,第3个组件填充第3行,第4个组件添加时,把第1,2个组件挤到第1行,第3个和第4个就占据第2行。以此类推,第5,6个组件填充第三行,当第7个组件添加时,第1,2,3个组件就占据了第一行,后面的就不说了......

           d.GridLayout在添加组件的时候,在add方法中设置插入的位置可以改变组件的位置,网格第一个组件只有默认位置0可以占据,第二个组件有0和1两个位置可以填充,依次类推。如果填写一个错误的位置,如:第三个组件填写插入的位置为3,程序会报错。当后面的组件占据前面的组件的位置时,被占据的组件往后移动一个位置,其他的依次往后移动一个位置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: