建立及中常见的布局管理器

public class LayoutFrame extends JFrame{

public LayoutFrame(){
//设置布局管理器为边框布局。将组件按东、西、南、北、中的方式进行放置。
//组件大小随着容器的大小而改变。
  this.setLayout(new BorderLayout());
  
  JButton bc = new JButton("中");
  this.add(bc,BorderLayout.CENTER);
  
  JButton eastButton = new JButton("东");
this.add(eastButton,BorderLayout.EAST);

  JButton westButton = new JButton("西");
this.add(westButton,BorderLayout.WEST);
  
  JButton southButton = new JButton("南");
this.add(southButton,BorderLayout.SOUTH);

  JButton northButton = new JButton("北");
  this.add(northButton,BorderLayout.NORTH);

//设置布局管理器为流式布局。将组件按从上到下,从左到右的方式排列。
//组件超出容器自行换行,组件不会因为容器的大小变化而变化。流式布局是JPanel默认布局方式
  this.setLayout(new FlowLayout());

  this.add(new JButton("1"));
  this.add(new JButton("2"));
  this.add(new JButton("3"));
  this.add(new JButton("4"));
  this.add(new JButton("5"));
this.add(new JButton("6"));


//设置布局管理器为网络布局。将容器等分成大小相等的几个部分,每个部分放一个组件
//当容器大小改变,组件大小随之改变
this.setLayout(new GridLayout(3,2));

this.add(new JButton("1"));
this.add(new JButton("2"));
this.add(new JButton("3"));
this.add(new JButton("4"));
this.add(new JButton("5"));
this.add(new JButton("6"));

this.setSize(600, 400);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
}


public static void main(String[] args) {
LayoutFrame a = new LayoutFrame();
}

}

原文地址:https://www.cnblogs.com/chenxiaoping/p/5360718.html