Java Swing:dispose()JFrame不会清除其控件 [英] Java Swing: dispose() a JFrame does not clear its controls

查看:408
本文介绍了Java Swing:dispose()JFrame不会清除其控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个closeWindow()方法,该方法使用dispose()来关闭当前JFrame.当我再次显示该窗口时,控件(文本框,列表,表等)仍然具有它们以前的值,而这些值是我在dispose():d the frame ...那里存在的.为什么?有没有其他方法可以完全关闭并清除框架?

I have a closeWindow() method which uses dispose() for the current JFrame to close down. When I show the window again, the controls (textboxes, lists, tables etc.) still have their previous values in place that were there when I dispose():d the frame... Why is that? Is there another way to completley close and clear a frame?

这是另一个JFrame用于显示另一个窗口的代码,我在这里做错了吗?

This is the code that another JFrame uses to show the other window, am I doing something wrong here?

@Action
public void showAddProductToOrderView() {

    if (addProductToOrderView == null) addProductToOrderView = new AddProductToOrderView(this);
    addProductToOrderView.setVisible(true);
}

推荐答案

设置窗口不会清除其子文本组件.处置将释放本机资源. java.awt.Window的Javadoc 还指出:

Disposing a window will not clear its child text components. Dispose will release native resources. The javadoc for java.awt.Window also states:

通过重建本机资源并随后调用pack或show,可以再次显示Window及其子组件.重新创建的Window及其子组件的状态将与放置Window时这些对象的状态相同(不考虑这些动作之间的其他修改).
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.

As suggested by others, create a new instance each time instead. If that's to expensive I believe your best option is to clear sub components when the view becomes visible, e.g. by overriding setVisible.

删除空检查,以每次创建一个新框架.

Remove the null check to create a new frame each time.

@Action
public void showAddProductToOrderView() {
    addProductToOrderView = new AddProductToOrderView(this);
    addProductToOrderView.setVisible(true);
}

我不知道您其余的代码,是否还有其他内容取决于所重用的框架.例如,如果您已附加了侦听器,请确保未注册它们以免泄漏它们.

I don't know about the rest of your code, if there's something else depending on the frame being reused. For example, if you have attached listeners, ensure they are unregistered to not leak them.

这篇关于Java Swing:dispose()JFrame不会清除其控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆