初学java之面板布局的控制

时间:2022-11-18 01:07:39
 /*
* 联系网格的布控laytout
*以一个棋盘为列子吧!
*/
import javax.swing.*;
import java.awt.*; class WindGrid extends JFrame
{
final int maxn =12;
GridLayout grid; //设置一个网络对象
JPanel myjpanel; //设置一个画板Jpanel
Label myLabel[][] ;
public WindGrid()
{
grid = new GridLayout(12,12); //设置行列
myjpanel = new JPanel();
myjpanel.setLayout(grid);
myLabel = new Label [maxn][maxn];
for(int i=0;i<maxn ;i++)
{
for(int j=0 ; j<maxn ;j++)
{
myLabel[i][j] = new Label("Gxjun");
//设置背景颜色
if((i+j)%2==0)
myLabel[i][j].setBackground(Color.black);
else
myLabel[i][j].setBackground(Color.white);
myjpanel.add(myLabel[i][j]); //将标签放置到面板上去
}
}
add(myjpanel,BorderLayout.CENTER);
//添加几个按钮
JButton mybutton;
mybutton=new JButton("北方参战国");
mybutton.setSize(2,3);
add(mybutton,BorderLayout.NORTH); setTitle("国际象棋");
setBounds(10,10,310,290);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //注销程序
validate(); //可法即可执行
}
} public class test
{
public static void main(String [] args)
{
new WindGrid();
}
}

面板控制一般需要用到setLayout ,GridLayout 这两个类....