使用swing绘制java网格 [英] Drawing java grid using swing

查看:343
本文介绍了使用swing绘制java网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用java绘制网格(10x10),但我们必须在 JFrame中使用 drawRectMethod 实现
/ code>,这是我到目前为止的程序

I want to draw a grid(10x10) using java,but we have to implement it using drawRectMethod in a JFrame,This is my program so far

import java.awt.*;
import javax.swing.*;

public class Grid extends JFrame {

    public Grid() {
        setSize(500, 500);
        setVisible(true);
    }

    // draw grid
    public void paint(Graphics g) {
        for (int x = 30; x <= 300; x += 30)
            for (int y = 30; y <= 300; y += 30)
                g.drawRect(x, y, 30, 30);
    }

    public static void main(String args[]) {
        Grid application = new Grid();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


推荐答案

此代码正常运行。

This code is working.

只需删除25

import java.awt.*;
 import javax.swing.*;
 public class Grid extends JFrame {

 public Grid()    {       
 setSize( 500, 500 );
 setVisible( true );   
 } 
public void paint( Graphics g )    
 {  
 for ( int x = 30; x <= 300; x += 30 )
 for ( int y = 30; y <= 300; y += 30 ) 
 g.drawRect( x, y, 30, 30 );

 } 
 public static void main( String args[] ) 
 {
     Grid application = new Grid();
 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   }  } 

这篇关于使用swing绘制java网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆