流式布局:按照组件的添加顺序从左到右放置在容器中,当达到容器的边界时将部件放置在下一行中。FlowLayout可以是左对齐,右对齐,居中对齐的方式排列组件
流式布局FlowLayout的注意事项
1.不限制它所管理的组件大小,允许他们有最佳大小
2.当容器被缩放时,组建的位置肯呢个变化,但是组件大小不变
3.默认组件是居中对齐,可以通过FlowLayout(int aligh)函数实现对齐方式
FlowLayout的5种对齐方式
FlowLayout.LEFT ——左对齐
FlowLayout.RIGHT ——右对齐
FlowLayout.CENTER—–居中对齐
FlowLayout.LEADING ——与开始一边对齐
FlowLayout.TRAILING——-与结束一边对齐
package com.hanshunpin.layout30; import javax.swing.JButton; import javax.swing.JFrame; import java.awt.*; import javax.swing.*; public class demo02 extends JFrame{ /** * @param args */ //定义需要的组件 JButton jb1,jb2,jb3,jb4,jb5,jb6,jb7; public static void main(String[] args) { // TODO Auto-generated method stub demo02 demo=new demo02(); } //构造函数 public demo02(){ // TODO Auto-generated constructor stub //创建组件 jb1=new JButton("按钮1"); //jb1.setSize(50, 50); jb2=new JButton("按钮2"); jb3=new JButton("按钮3"); jb4=new JButton("按钮4"); jb5=new JButton("按钮5"); jb6=new JButton("按钮6"); jb7=new JButton("按钮7"); //添加组件 this.add(jb1); this.add(jb2); this.add(jb3); this.add(jb4); this.add(jb5); this.add(jb6); this.add(jb7); this.setLayout(new FlowLayout(FlowLayout.LEFT)); //这样设置就可以改变按钮的大小,否则jb1.setSize(50, 50);就会没有效果 //this.setLayout(null); //对窗体进行设置 this.setTitle("布局设置"); this.setSize(300,300); this.setLocation(100, 100); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //禁止用户改变窗体大小 this.setResizable(false); } }