常见的布局有 绝对布局,流式布局,边界布局和网格布局,具体的实现办法如下所示:
1:局对布局:

package demo;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Layoutt extends JFrame{
    public Layoutt(){
        this.setTitle("AbsoluteLayout");
        this.setLayout(null);
        this.setBounds(100,100,1000,1000);
        Container  con = this.getContentPane();
        JButton j1 = new JButton("button1");
        JButton j2 = new JButton("button2");
        j1.setBounds(110, 110, 100, 30);
        j2.setBounds(110, 210, 100, 30);
        con.add(j1);
        con.add(j2);
        j1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                System.out.println("test");
            }
        });
        this.setVisible(true);
    }
    public static void main(String [] args){
        new Layoutt();
    }
}

java的布局设计 java的布局方式_java


边界布局的缺点是在图形变化的过程中,由于每个窗体元素的位置是固定的所以在具体的操作中不够灵活,但是一下的三种布局情况是紧紧跟随窗口大小的不会出现绝对布局中的那种情况。

2.流式布局:实现代码如下:

package demo;

import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

//这种布局模式需要手动设置
public class LayOut1 extends JFrame{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public LayOut1(){
        super("流式布局");
        Container con = getContentPane();
        this.setLayout(new FlowLayout(2,10,10));
        this.setVisible(true);
        this.setLocation(200, 200);
        this.setSize(300,200);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        for(int a = 1;a<=9;a++){
            con.add(new JButton("Button"+a));
        }
    }
    public static void main(String [] args){
        new LayOut1();
    }
}

操作结果如下:

java的布局设计 java的布局方式_网格布局_02


3.边界布局:代码如下:

package demo;

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;



@SuppressWarnings("serial")
public class BorderLayOutDemo  extends JFrame{

    //BorderLayout bd = new BorderLayout();
    String[] border = {BorderLayout.CENTER,BorderLayout.SOUTH,BorderLayout.NORTH,BorderLayout.EAST,
            BorderLayout.WEST};
    String[] buttonName={"centerButton","southButton","northButton","eastButton","westButton"};
    public BorderLayOutDemo(){
        super("BorderLayout");
        this.setSize(350, 230);
        this.setLayout(new BorderLayout());
        Container con = this.getContentPane();
        for(int i = 0;i<border.length;i++){
            con.add(border[i],new JButton(buttonName[i]));
        }

        this.setVisible(true);
        //设置窗口关闭方式
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
    public static void main(String [] args){
        new BorderLayOutDemo();
    }
}

结果如下:

java的布局设计 java的布局方式_java_03


4.网格布局:

4.网格布局:
package demo;

import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class LayOut4 extends JFrame{
    public LayOut4(){
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        Container con = this.getContentPane();
        this.setSize(500, 500);
        //后面的两个参数是这的窗体中的元素之间的互相的距离
        this.setLayout(new GridLayout(7,3,1,5));
        for(int a = 1;a<=20;a++){
            con.add(new JButton("Button"+a));
        }
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
    public static void main(String [] args){
        new LayOut4();
    }
}

结果如下:

java的布局设计 java的布局方式_流式布局_04