如何释放JFrame资源?

时间:2016-01-01 23:50:39

标签: java swing

我有一个问题:
setVisible(false)上拨打JFrame,会使其消失,对吧? 再次调用setVisible(true)使它再次出现,这意味着该对象完全仍在RAM中,对吧? 如果我需要在按下“关闭”按钮后立即删除所有内容,那么正确的方式是什么(当我在主UI中按下“配置”按钮时显示JFrame,所以它应该消失(同样来自RAM)当我按下关闭按钮)?
谢谢你的时间,抱歉我的英语不好

2 个答案:

答案 0 :(得分:1)

使用JFrame的可见性不允许操作系统回收窗口分配的本机资源,这样做的方法是调用JFrame#dispose

/**
 * Releases all of the native screen resources used by this
 * {@code Window}, its subcomponents, and all of its owned
 * children. That is, the resources for these {@code Component}s
 * will be destroyed, any memory they consume will be returned to the
 * OS, and they will be marked as undisplayable.
 * ...
 */
public void dispose() {
    doDispose();
} 

关闭JFrame时调用dispose的另一种方法是执行以下操作

frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

答案 1 :(得分:0)

您必须设置框架的默认关闭操作。

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

See this link

您也可以在框架关闭/丢弃后尝试清理。

找到相关的documentation here

相关问题