java 布局之FlowLayout

FloswLayout是每个JPanel对象的默认布局管理器,它就是按照顺序行云流水般的把所有的组件一个一个的画上去。例子很简单

/*
 * BoxLayoutDemo.java requires no other files.
 */
 
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
public class BoxLayouts extends JFrame{
	
	private JButton btn1 = new JButton("Button1");
	private JButton btn2 = new JButton("Button2");
	private JButton btn3 = new JButton("Button3 what a fine day");
	private JButton btn4 = new JButton("Button4 what a fine da");
	private JButton btn5 = new JButton("Button5 what a fine d");
	private JButton btn6 = new JButton("Button6 what a fines");
	private JButton btn7 = new JButton("Button7");
	private JButton btn8 = new JButton("Button8");
	private JButton btn9 = new JButton("Button3 what a fine day");
	private JButton btn10 = new JButton("Button4 what a fine da");
	private JButton btn11 = new JButton("Button5 what a fine d");
	private JButton btn12 = new JButton("Button6 what a fines");
	BoxLayouts()
	{
		super();
		initComponent();
		
	}
	private void initComponent()
	{
		Container container = this.getContentPane();
		//container.setLayout(new BoxLayout(container,BoxLayout.LINE_AXIS));
		container.setLayout(new FlowLayout());
		container.add(btn1);
		container.add(btn2);
		container.add(btn3);
		container.add(btn4);
		container.add(btn5);
		container.add(btn6);
		container.add(btn7);
		container.add(btn8);
		container.add(btn9);
		container.add(btn10);
		container.add(btn11);
		container.add(btn12);
	}
	
	public static void main(String[] args)
	{
		BoxLayouts frame = new BoxLayouts();
		frame.pack();
		frame.setVisible(true);
	}
	
}
程序运行结果:



你可能感兴趣的:(java,布局,FlowLayout)