Graphics#drawText,旧文本不会被删除
我有一个 JPanel ,它像这样覆盖 PaintComponent
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//[...]
g.drawString(" x " + model.getCount(l), getTilesWidth() + ship.getWidth() + PREVIEW_OFFSET_X + 5, y - 10);
//[...]
}
,但是当我调用 repaint 且 model.getCount(l) 已更改时,新字符串将绘制在旧字符串上方。然而,当我调整窗口大小时,一切又恢复正常了。造成这种情况的原因是什么?
I have a JPanel which overrides paintComponent like this
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//[...]
g.drawString(" x " + model.getCount(l), getTilesWidth() + ship.getWidth() + PREVIEW_OFFSET_X + 5, y - 10);
//[...]
}
but when I call repaint and model.getCount(l) has changed, the new string gets just drawn above the old string. However, when I resize the window everything is fine again. What could be the cause for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在尝试绘制新文本之前,您很可能需要用背景颜色填充旧文本所在的区域。
Most likely you need to fill in the area where the old text was with the background color before attempting to draw the new text.
哎呀,错误实际上是在其他地方,我真的忘记了我的类除了 JPanel 之外还有一个超类,对此感到抱歉。
ooops, the error was actually elsewhere, I really forgot that my class had a superclass other than JPanel, sorry for that.