日期:2014-05-20 浏览次数:20787 次
import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; public class Test extends JFrame{ private int height = 22; //格子的长度 private int width = 22; //格子的宽度 private int size = 20; //每个格子的大小 public Test(){ this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(height*size+10,width*size+35); Graphics g = this.getGraphics(); g.setColor(Color.blue); for(int i=0;i<width;++i){ for(int j=0;j<height;++j) g.fill3DRect(i*width, j*height, size, size, true); } } public static void main(String []args){ new Test(); } }
import java.awt.*; import javax.swing.*; public class Test extends JFrame{ public Test(){ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(new TestComponent()); this.pack(); this.setVisible(true); } public static void main(String []args){ new Test(); } } class TestComponent extends JPanel { private int height = 22; //格子的长度 private int width = 22; //格子的宽度 private int size = 20; //每个格子的大小 public TestComponent() { this.setPreferredSize(new Dimension(height*size+10,width*size+35)); //为了让pack起作用 } public void paintComponent(Graphics g) { g.setColor(Color.blue); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { g.fill3DRect(i * width, j * height, size, size, true); } } } }