视觉工件出现在JPanel上

时间:2012-11-08 01:23:26

标签: java swing jpanel repaint paintcomponent

我正在尝试使用JPanel创建一个包含2 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();
        }
    }
}

1 个答案:

答案 0 :(得分: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...
}

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

Graphics对象通过单个重绘传递在组件之间共享,未能遵守正确的颜料链将导致像您这样的问题

永远不要使用任何paint方法更新用户界面(包括调用repaint),这只会导致您的paint方法被一遍又一遍地召回。 .until你CPU达到100%并且程序挂起。

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

我建议您阅读: