1 /* 2 * 联系网格的布控laytout 3 *以一个棋盘为列子吧! 4 */ 5 import javax.swing.*; 6 import java.awt.*; 7 8 class WindGrid extends JFrame 9 { 10 final int maxn =12; 11 GridLayout grid; //设置一个网络对象 12 JPanel myjpanel; //设置一个画板Jpanel 13 Label myLabel[][] ; 14 public WindGrid() 15 { 16 grid = new GridLayout(12,12); //设置行列 17 myjpanel = new JPanel(); 18 myjpanel.setLayout(grid); 19 myLabel = new Label [maxn][maxn]; 20 for(int i=0;i<maxn ;i++) 21 { 22 for(int j=0 ; j<maxn ;j++) 23 { 24 myLabel[i][j] = new Label("Gxjun"); 25 //设置背景颜色 26 if((i+j)%2==0) 27 myLabel[i][j].setBackground(Color.black); 28 else 29 myLabel[i][j].setBackground(Color.white); 30 myjpanel.add(myLabel[i][j]); //将标签放置到面板上去 31 } 32 } 33 add(myjpanel,BorderLayout.CENTER); 34 //添加几个按钮 35 JButton mybutton; 36 mybutton=new JButton("北方参战国"); 37 mybutton.setSize(2,3); 38 add(mybutton,BorderLayout.NORTH); 39 40 setTitle("国际象棋"); 41 setBounds(10,10,310,290); 42 setVisible(true); 43 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //注销程序 44 validate(); //可法即可执行 45 } 46 } 47 48 public class test 49 { 50 public static void main(String [] args) 51 { 52 new WindGrid(); 53 } 54 }
面板控制一般需要用到setLayout ,GridLayout 这两个类....