JavaSwing布局GridLayout的用法

司徒墨竹
2023-12-01
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


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


public class GridLayoutDemo {

public GridLayoutDemo(){
JFrame f = new JFrame();
Container contentPane = f.getContentPane();
contentPane.setLayout(new GridLayout(6,1));//6行1列

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("GridLayout");
f.pack();
f.setVisible(true);

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

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


}

构造函数:

GridLayout() 默认1行1列

GridLayout(int rows,int cols) rows行,cols列

CridLayout(int rows,int cols,int hgap,int vgap) 设置组件之间的间距



 类似资料: