视觉工件出现在JPanel上

jav*_*oob 2 java swing jpanel repaint paintcomponent

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

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Mad*_*mer 7

你没打电话 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...
}
Run Code Online (Sandbox Code Playgroud)

您有义务致电super.paintComponent以确保正确维护油漆链,并进行不透明度和清理图形上下文等操作.

Graphics目的是通过一个单一的重绘通分量之间共享,不履行正确的涂料链将导致,以及,像您的问题

永远不要以任何paint方式更新UI (这包括调用repaint),这只会导致您的paint方法被一遍又一遍地重新调用......直到CPU达到100%并且程序挂起.

从来没有,在paint方法(或一般用户界面)中进行任何时间消耗或阻止操作,这将使程序看起来像挂起并让用户感到不安(你认为zombi成群很糟糕:P).以这种方式阻止会阻止EDT响应绘制请求...

我建议你仔细阅读: