我有一个closeWindow()方法,它使用dispose()来关闭当前的JFrame.当我再次显示窗口时,控件(文本框,列表,表格等)仍然具有它们之前的值,当我处理()时:框架……为什么会这样?是否有另一种方法来完成关闭和清除框架?
这是另一个JFrame用来显示另一个窗口的代码,我在这里做错了吗?
@Action
public void showAddProductToOrderView() {
if (addProductToOrderView == null) addProductToOrderView = new AddProductToOrderView(this);
addProductToOrderView.setVisible(true);
}
解决方法:
处置窗口不会清除其子文本组件. Dispose将释放本机资源. java.awt.Window的javadoc还指出:
The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed (not accounting for additional modifications between those actions).
正如其他人所建议的那样,每次都要创建一个新实例.如果这是昂贵的,我相信你最好的选择是在视图变得可见时清除子组件,例如通过重写setVisible.
编辑:
删除空检查以每次创建一个新框架.
@Action
public void showAddProductToOrderView() {
addProductToOrderView = new AddProductToOrderView(this);
addProductToOrderView.setVisible(true);
}
我不知道你的其余代码,如果还有别的东西取决于重用的框架.例如,如果您已连接侦听器,请确保它们未注册,以免泄露它们.
标签:java,swing,jframe
来源: https://codeday.me/bug/20190610/1214061.html