我在互联网上发现了类似的问题,但提供的解决方案不起作用。
我想清除JPanel。为此,我调用repaint()
(从clear()
方法)并将标志设置为 false 以避免调用我的绘图方法(drawLines()
)。画线仍在面板上。
我尝试用背景颜色重新绘制相同的线条。这也没有奏效。
public class WektPanel extends JPanel{
boolean check = false;
Color c = Color.BLUE;
boolean oval = false;
public WektPanel() {
setBackground(c);
setVisible(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(check)
drawLines(3, g);
}
void clear(){
check=false;
repaint();
}
void draw(){
check=true;
repaint();
}
void drawLines(int stroke, Graphics g){
g.drawLine(0,0,getWidth(),getHeight());
g.drawLine(0,getHeight(),getWidth(),0);
for(int i=1; i<stroke;i++){
g.drawLine(0+i,0,getWidth(),getHeight()-i);
g.drawLine(0,0+i,getWidth()-i,getHeight());
g.drawLine(0,getHeight()-i,getWidth()-i,0);
g.drawLine(0+i,getHeight(),getWidth(),0+i);
}
}
}