public class JPanelTest extends JFrame{
public JPanelTest(){
Container c=getContentPane();
c.setLayout(new GridLayout(2,1,10,10));
//初始化一个面板,设置一行三列的网格布局
JPanel p1=new JPanel(new GridLayout(3, 1, 10, 10));
JPanel p2=new JPanel(new GridLayout(1, 2, 10, 10));
JPanel p3=new JPanel(new GridLayout(1, 2, 10, 10));
JPanel p4=new JPanel(new GridLayout(2, 1, 10, 10));
p1.add(new JButton("1"));
p2.add(new JButton("2"));
p3.add(new JButton("3"));
p4.add(new JButton("4"));
c.add(p1);
c.add(p2);
c.add(p3);
c.add(p4);
setTitle("在这个里面使用了面板");
setVisible(true);
setBounds(0, 0, 500, 600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelTest();
}
}
原文:http://www.cnblogs.com/douyunqian3520/p/5517915.html