Java 函数内部的组件

Java 函数内部的组件,java,paintcomponent,Java,Paintcomponent,我是一个非常初学者,所以我认为我犯了一个愚蠢的错误,但我有: public class Draw extends JPanel { private static final long serialVersionUID = 1L; public Draw(int rx, int ry) { public void paintComponent(Graphics g) { super.paintComponent(

我是一个非常初学者,所以我认为我犯了一个愚蠢的错误,但我有:

public class Draw extends JPanel {

    private static final long serialVersionUID = 1L;

    public Draw(int rx, int ry)
    {

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            this.setBackground(Color.WHITE);
            g.setColor(Color.BLUE);
            try{
                g.fillRect(rx, ry, 5, 5);
                Thread.sleep(1000);
            }
            catch(InterruptedException ex)
            {
                Thread.currentThread().interrupt();
            }

        }
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("Title");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        for (int i = 1; i < 40; i++) {
            Random r = new Random();
            int rx = r.nextInt(40);
            int ry = r.nextInt(40);

            Draw d = new Draw(rx, ry);
            f.add(d);
            f.setSize(400, 400);
            f.setVisible(true);
        }
    }
}
公共类Draw扩展了JPanel{
私有静态最终长serialVersionUID=1L;
公共提款(整数rx,整数ry)
{
公共组件(图形g)
{
超级组件(g);
这个.背景(颜色.白色);
g、 setColor(Color.BLUE);
试一试{
g、 fillRect(rx,ry,5,5);
睡眠(1000);
}
捕获(中断异常例外)
{
Thread.currentThread().interrupt();
}
}
}
公共静态void main(字符串[]args){
JFrame f=新JFrame(“标题”);
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
对于(int i=1;i<40;i++){
随机r=新随机();
int rx=r.nextInt(40);
int-ry=r.nextInt(40);
图纸d=新图纸(rx,ry);
f、 加(d);
f、 设置大小(400400);
f、 setVisible(真);
}
}
}
我希望代码在随机位置生成矩形,每个循环中都有一些延迟。 Java正在写“void对于变量paintComponent是无效的类型”。我不相信这一点,因为如果我把paintComponent放在Draw函数之外,它就可以工作了(但我不想让他工作)。
paintComponent是否不能位于其他函数内或存在其他错误?

以下是您想要的示例:

您不应该在其他方法(如构造函数)中定义方法。如果只需要创建间隔,也不要使用线程。改用
javax.swing.Timer

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Draw extends JPanel {

    private static final int FRAME_HEIGHT = 400;

    private static final int FRAME_WIDTH = 400;

    private static final int RECT_WIDTH = 40;

    private static final int RECT_HEIGHT = 25;


    private static final long serialVersionUID = 1L;

    public Draw()
    {
        this.setBackground(Color.WHITE);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        //
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLUE);

        Random r = new Random();
        int rx = r.nextInt(FRAME_WIDTH-RECT_WIDTH);
        int ry = r.nextInt(FRAME_HEIGHT-RECT_HEIGHT);
        g.fillRect(rx, ry, RECT_WIDTH, RECT_HEIGHT);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("Title");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final Draw d = new Draw();
        f.add(d);
        f.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        f.setVisible(true);

        Timer t = new Timer(1000, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                d.repaint();
            }
        });
        t.start();
    }
}

祝你好运。

将该方法置于构造函数之外。