[英]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 我将此类添加到另一个JPanel中
Grid grid = new Grid(dim);
parentPanel.add(grid);
By default, the grid is hidden ( grid.hide();
) so my jFrame looks something like this: 默认情况下,网格是隐藏的( grid.hide();
),所以我的jFrame看起来像这样:
where the "blue" area is the parentPanel and the "orange" area is a JPanel with buttons. 其中“蓝色”区域是parentPanel,而“橙色”区域是带有按钮的JPanel。 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.