java 调整大小时重新绘制框架?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1569463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Repaint frame when resized?
提问by joef
How would I force a Frame to repaint()immediately after it is maximized, or resized?
如何repaint()在最大化或调整大小后立即强制框架?
I can't find what method is called when that specific operation takes place. I have a bunch of graphics that are written with Graphic objects in paint and their orientation depends on the real time feedback from getWidth()and getHeight()but paint isn't called when I maximize, only when those pixels change unfortunately.
我找不到特定操作发生时调用的方法。我有一堆图形,它们是用 Paint 中的 Graphic 对象编写的,它们的方向取决于来自的实时反馈getWidth(),getHeight()但是当我最大化时不会调用 Paint,只有当这些像素不幸发生变化时才调用。
回答by Jonathan Feinberg
Register a ComponentListener:
注册一个组件监听器:
final Component myComponent = makeTheFrameOrWhatever();
myComponent.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e)
{
myComponent.repaint();
}
});
回答by Lawrence Dol
You need to dig further because repaint() and paint() most certainly arecalled when you resize a frame - it's the only way the frame can cause it's contents to be painted. It is most likely that you're not seeing the repaint reach your specific component in the frame, perhaps because the particular layout you are using is not affected if the window is made larger. Or if you have no layout, then you must subclass Frame and explicitly have it call paint on the subcomponents, since there is no layout manager to do that for you.
您需要进一步挖掘,因为在调整框架大小时肯定会调用repaint() 和paint() - 这是框架可以导致其内容被绘制的唯一方式。您很可能没有看到重绘到达框架中的特定组件,这可能是因为如果窗口变大,您正在使用的特定布局不会受到影响。或者,如果您没有布局,那么您必须子类化 Frame 并明确地让它在子组件上调用paint,因为没有布局管理器可以为您执行此操作。
Note that whether or not painting is done repeatedly whileyou are resizing, as opposed to just once after you let go the mouse button may be an operating system option.
请注意,在调整大小时是否重复绘制,而不是在松开鼠标按钮后仅绘制一次,这可能是操作系统选项。

