通过drawRect,谢谢了。

解决方案 »

  1.   

     void drawRect(int x, int y, int width, int height) 
     
      

  2.   

    Component都有getGraphics()方法
    然后再用得到的Graphics来
    drawRect(int x, int y, int width, int height) 
      

  3.   


    import java.awt.*;
    public class Test extends Frame{
    public static void main(String[] args) {
    new Test().launch();
    }

    public void launch() {
    setBounds(50,50,300,300);
    setVisible(true);
    }

    public void paint(Graphics g) {
    Color c = g.getColor();
    g.setColor(Color.RED);
    g.drawRect(20, 30, 50, 80);
    g.setColor(c);
    }
    }
      

  4.   


    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */package experiment16;
    import java.awt.*;
    import javax.swing.*;/**
     *
     * @author Kevin Zhang
     */
    class DrawPanel extends JFrame {
        DrawPanel(String s) {
            super(s);
            setSize(400, 400);
            setVisible(true);
        }
        // 在面板中绘制图形
        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.BLACK);            // 把当前的前景色设置成黑色
            g.drawRect(-150, -150, 100, 100);   // 绘制一个正方形
        }
    }
    public class Main {
        public static void main(String[] args) {
            DrawPanel drawPanel = new DrawPanel("绘制矩形");
            drawPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
      

  5.   


    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */package experiment16;
    import java.awt.*;
    import javax.swing.*;/**
     *
     * @author Kevin Zhang
     */
    class DrawPanel extends JFrame {
        DrawPanel(String s) {
            super(s);
            setSize(400, 400);
            setVisible(true);
        }
        // 在面板中绘制图形
        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.BLACK);            // 把当前的前景色设置成黑色
            g.drawRect(-150, -150, 100, 100);   // 绘制一个正方形
        }
    }
    public class Main {
        public static void main(String[] args) {
            DrawPanel drawPanel = new DrawPanel("绘制矩形");
            drawPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }