JavaSwing布局FlowLayout的用法

来源:互联网 发布:初中网络课程 编辑:程序博客网 时间:2024/06/05 17:31
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


import javax.swing.*;


public class FlowLayoutDemo {

public FlowLayoutDemo(){
JFrame f = new JFrame();
Container contentPane = f.getContentPane();
contentPane.setLayout(new FlowLayout());

contentPane.add(new JButton("first"));
contentPane.add(new JButton("second"));
contentPane.add(new JButton("third"));
contentPane.add(new JButton("fourth"));
contentPane.add(new JButton("fifth"));
contentPane.add(new JButton("This is the last button"));

f.setTitle("FlowLayout");
f.setSize(getPreferredSize());//getPreferredSize()获取当前窗口大小
f.setVisible(true);

f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

}

private Dimension getPreferredSize() {
// TODO Auto-generated method stub
return new Dimension(200,200);
}


public static void main(String[] args) {
// TODO Auto-generated method stub
FlowLayoutDemo b = new FlowLayoutDemo();
}


}


构造函数:

FlowLayout() 建立一个新的FlowLayout,默认居中对齐,组件彼此有5单位的水平与垂直间距

FlowLayout(int align) 建立一个新的FlowLayout,可设置排列方式,组件彼此有5单位的水平与垂直间距

FlowLayout(int align,int hgap,int vgap) 此FlowLayout可设置排列方式与组件间距


五种排列方式:

CENTER,LEFT,RIGHT,LEADING,TRAILING

例:contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));


0 0