JPanel启动
p = new JPanel() {
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g) {
if(errors == 1)
g.drawOval(215, 50, 75, 75);
else if(errors == 2)
g.drawOval(200,200,200,200);
}
};
调用的方法repaint
static void drawHead() {
System.out.println("Head");
errors = 1;
p.removeAll();
p.revalidate();
p.repaint();
}
Before repaint
我的框架看起来像这样https://i.stack.imgur.com/T8F0V.png https://i.stack.imgur.com/T8F0V.png
然后看起来像这样,https://i.stack.imgur.com/UFwr0.png https://i.stack.imgur.com/UFwr0.png
我想我的有一个错误drawHead()
方法,但我似乎无法解决问题。有谁知道发生了什么事吗?我想要的结果是第一张图像,但带有head
被吸引进来。
你因为没有打电话而破坏了油漆链super.paintComponent
在进行任何定制绘画之前首先
Graphics
是共享资源,在绘制周期中绘制的每个组件都将共享相同的资源Graphics
上下文,这意味着之前绘制的任何内容Graphics
除非您清除它,否则上下文将保留。
的工作之一是paintComponent
是要准备Graphics
通过用组件的背景颜色填充它来绘制上下文
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)