简体   繁体   中英

Strange jpanel paint

here is my class

public class Grid extends JPanel{
    public Grid(Dimension dim){
        this.dim = dim;
        step = 0.02;
        this.setSize(dim);
        this.setLocation(0, 0);
    }

    @Override
    public void paint(Graphics g){
        for(int i=-1; i<this.getHeight(); i+=this.getHeight()*step){
            g.drawLine(0, i, this.getWidth(), i);
        }
        for(int i=-1; i<this.getWidth(); i+=this.getWidth()*step){
            g.drawLine(i, 0, i, this.getHeight());
        }
    }
}

(I skipped the definition of class variables)

I add this class in another JPanel like this

Grid grid = new Grid(dim);
parentPanel.add(grid);

By default, the grid is hidden ( grid.hide(); ) so my jFrame looks something like this: 在此处输入图片说明

where the "blue" area is the parentPanel and the "orange" area is a JPanel with buttons. The problem is that when I press the grid button, I get the following result: 在此处输入图片说明

where the grid lines are ok, but I get also this orange box which I do not know why is there.

Any ideas?

我必须在构造函数中添加以下行

this.setOpaque(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM