且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java Swing revalidate() 与 repaint()

更新时间:2023-12-03 23:05:52

你需要调用repaint()revalidate().前者告诉 Swing 窗口的某个区域是脏的(这是擦除 removeAll() 删除的老孩子的图像所必需的);后者告诉布局管理器重新计算布局(这在添加组件时是必需的).这应该会导致面板的子项重新绘制,但可能不会导致面板本身重新绘制(请参阅 this 用于重绘触发器列表).

You need to call repaint() and revalidate(). The former tells Swing that an area of the window is dirty (which is necessary to erase the image of the old children removed by removeAll()); the latter tells the layout manager to recalculate the layout (which is necessary when adding components). This should cause children of the panel to repaint, but may not cause the panel itself to do so (see this for the list of repaint triggers).

更一般的注意事项:与其重用原始面板,我建议您构建一个新面板并在父面板上交换它们.

On a more general note: rather than reusing the original panel, I'd recommend building a new panel and swapping them at the parent.