package test1;
import java.awt.*;
import java.awt.Color;
import javax.swing.*;
public class GirdLayout_1 extends JFrame {
String[] str = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
public GirdLayout_1(){
super("网格布局");
// this.getContentPane().setBackground(Color.BLACK);
this.getContentPane().setBackground(Color.GRAY);
this.setBounds(200, 200, 500, 500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new GridLayout(3, 3));
for(int i=0;i<str.length;i++){
if(i==4){
JPanel jp = new JPanel();
// FlowLayout fl = new FlowLayout(FlowLayout.CENTER ,20, 20);
// jp.setLayout(fl);
// GridLayout gl = new GridLayout(2, 2);
// jp.setLayout(gl);
BorderLayout bl = new BorderLayout();
jp.setLayout(bl);
for(int j=4;j<str.length;j++){
switch(j){
case 4: jp.add(new JButton(str[j]), BorderLayout.EAST);break;
case 5: jp.add(new JButton(str[j]), BorderLayout.SOUTH);break;
case 6: jp.add(new JButton(str[j]), BorderLayout.NORTH);break;
case 7: jp.add(new JButton(str[j]), BorderLayout.WEST);break;
case 8: jp.add(new JButton(str[j]), BorderLayout.CENTER);break;
}
}
this.getContentPane().add(jp);
}
else
this.getContentPane().add(new JButton(str[i]));
}
this.setVisible(true);
}
public static void main(String arg[]){
new GirdLayout_1();
}
}