用JAVA编写一个程序绘制一个10*10的网格,每格(正方形)边长为20像点。

来源:百度知道 编辑:UC知道 时间:2024/06/27 06:39:26

import java.awt.*;
import javax.swing.*;
public class DrawRectTest extends JFrame
{
public DrawRectTest()
{
GPanel g=new GPanel();
this.getContentPane().add(g);
this.setSize(500,500);
this.setVisible(true);
}
public static void main(String[] args)
{
new DrawRectTest();

}

}
class GPanel extends JPanel
{
public void paintComponent(Graphics g)
{
int w;//横坐标
int h;//纵坐标
for(int i=1;i<=10;i++)
{
h=30;
w=10;
for(int j=1;j<=10;j++)
g.drawRect(w=w+20, h, 20, 20);
}
}
}