简体   繁体   English

Java重绘不能正常工作

[英]Java repaint not working correctly

i use the java repaint method , it repaints, but the update is only seen when I either click on the canvas or resize the panel. 我使用java重绘方法,它重新绘制,但只有当我点击画布或调整面板大小时才会看到更新。 How can I fix this ? 我怎样才能解决这个问题 ? What causes it? 是什么导致的?

You need to call the method revalidate(). 您需要调用方法revalidate()。 This forces the layout manager to update / repaint all its components. 这会强制布局管理器更新/重新绘制其所有组件。

repaint()实际上并没有重新绘制,它只是要求重新绘制组件。

It may be helpful to simply grab the Graphics object from the component you wish to paint. 简单地从您想要绘制的组件中获取Graphics对象可能会有所帮助。 Then just invoke a paint method on the Graphics object. 然后只需在Graphics对象上调用paint方法。 For example: 例如:

g = component.getGraphics(); 
draw(g);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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