Java JPanel上出现的视觉伪影

Java JPanel上出现的视觉伪影,java,swing,jpanel,repaint,paintcomponent,Java,Swing,Jpanel,Repaint,Paintcomponent,我正在尝试使用BorderLayout使用2JPanel创建一个程序。中间面板用于随机绘制矩形,而南面板用于按钮 每当我将鼠标光标悬停在“北”或“南”按钮上时,就会在JFrame的左上角看到一个奇怪的按钮图像。我做了一些研究,发现这可能是拥有透明背景的原因。我尝试对面板使用super.paintComponent(g),但前面绘制的其余矩形消失了。我需要矩形保持在JPanel中,但不需要左上角的奇怪图像 我不知道我做错了什么,希望有人能帮助或提供一些关于如何解决这个问题的线索 publi

我正在尝试使用
BorderLayout
使用2
JPanel
创建一个程序。中间面板用于随机绘制矩形,而南面板用于按钮

每当我将鼠标光标悬停在“北”或“南”按钮上时,就会在
JFrame
的左上角看到一个奇怪的按钮图像。我做了一些研究,发现这可能是拥有透明背景的原因。我尝试对面板使用
super.paintComponent(g)
,但前面绘制的其余矩形消失了。我需要矩形保持在
JPanel
中,但不需要左上角的奇怪图像

我不知道我做错了什么,希望有人能帮助或提供一些关于如何解决这个问题的线索

    public class TwoBRandomRec extends JFrame{

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        TwoBRandomRec rec = new TwoBRandomRec();

        rec.setSize(500,500);
        rec.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        rec.setVisible(true);
    }

    public TwoBRandomRec() {
        //Create the buttons
        JButton north = new JButton("North");
        JButton south = new JButton("South");
        DrawPanel drawPanel = new DrawPanel(500,500);

        JPanel southP = new JPanel();
        southP.add(south);
        southP.add(north);

        this.add(drawPanel, BorderLayout.CENTER);
        this.add(southP, BorderLayout.SOUTH);

        this.setTitle("TwoButtonRandomRec");
        this.pack();        
    }

    public class DrawPanel extends JPanel {

        private static final long serialVersionUID = 1L;

        private Random rand = new Random();
        private int x,y,xSize,ySize;
        private int height,width;

        public DrawPanel(int w,int h) {
            width = w;
            height = h;
        }
        public void RandomX(){
             x=rand.nextInt(width-1);
             xSize=rand.nextInt(width-x);
         }

         public void RandomY(){
             y=rand.nextInt(height-1);
             ySize=rand.nextInt(height-y);
         }

         public Color RandomColour(){
             rand.nextInt(height);
             return new Color(rand.nextInt(255),rand.nextInt(255),rand.nextInt(255));
         }

        @Override
        protected void paintComponent(Graphics g) {
            RandomX();
            RandomY();

            g.setColor(RandomColour());
            g.fillRect(x, y, xSize, ySize);
            try {
                Thread.sleep(50);

            } catch (InterruptedException e) {  
            }

            repaint();
        }
    }
}

您没有调用
super.paintComponent

protected void paintComponent(Graphics g) {
    super.paintComponent(g); // <-- Insert me here and watch problem go away
    RandomX();
    RandomY();

    g.setColor(RandomColour());
    g.fillRect(x, y, xSize, ySize);
    try {
        Thread.sleep(50); // <-- This is an INCREDIBLY bad idea, NEVER, EVER do this

    } catch (InterruptedException e) {  
    }

    repaint(); // <-- This is a bad idea, watch CPU max out...
}
受保护的组件(图形g){

super.paintComponent(g);//请学习java命名约定并遵守它们。感谢您的帮助!但是,super.paintComponent(g)清除我的整个屏幕。我还需要显示以前绘制的矩形。知道怎么做吗?每次重新绘制都需要重新呈现其内容,从头开始,这可以确保组件正确地处于最新状态,而不管请求重新绘制的原因是什么。您必须维护所有矩形的列表并重新绘制在每次通过时都将其替换为nt。或者,您可以将其绘制到屏幕外缓冲区,然后简单地将其绘制到acreen Inatad。这就是绘制工作的方式查看一个示例,这正是我要寻找的!过去几天我一直在为此绞尽脑汁!非常感谢您提供的所有参考资料。非常感谢!