日期:2014-05-20 浏览次数:20907 次
package test; import java.awt.*; import javax.swing.*; public class TestGrid extends JFrame{ public static void main(String[] args) { TestGrid game = new TestGrid(); game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); game.setVisible(true); } public TestGrid(){ super(); this.setTitle("just a test!"); this.setSize(600, 400); this.getContentPane().setLayout(new BorderLayout()); DrawGrid(resultPanel); this.add(resultPanel,BorderLayout.CENTER); } private void DrawGrid(JPanel panel) { width = panel.getWidth(); height = panel.getHeight(); section_width = width / 20; section_height = height / 20; remainder_width = width - section_width * 20; remainder_height = height - section_height * 20; Graphics graphics = panel.getGraphics(); graphics.clearRect(0, 0, width, height); for (int i = 0; i <= 20; i++) { graphics.drawLine(remainder_width / 2 + section_width * i, remainder_height / 2, remainder_width / 2 + section_width * i, height - remainder_height / 2); } for (int i = 0; i <= 20; i++) { graphics.drawLine(remainder_width / 2, remainder_height / 2 + section_height * i, width - remainder_width / 2, remainder_height / 2 + section_height * i); } } private int width ; private int height ; private int section_width; private int section_height; private int remainder_width; private int remainder_height; private JPanel resultPanel = new JPanel(); }