简体   繁体   English

处置一个JFrame

[英]Dispose a JFrame

I'm trying to dispose my JFrame with the following method: 我正在尝试使用以下方法处置JFrame:

private void killJFrame(JFrame jFrame) {
    SwingUtilities.invokeLater(() -> jFrame.dispose());
}

When killJFrame() is executed, the JFrame becomes invisible and it seems like it worked. 当执行killJFrame()JFrame变得不可见,并且看起来可以正常工作。 However, when calling Frame.getFrames() , the returned array still contains all of the JFrame s which were created in my application. 但是,当调用Frame.getFrames() ,返回的数组仍包含在我的应用程序中创建的所有JFrame

There is no difference if I try it like this: 如果我这样尝试,没有区别:

private void killJFrame(JFrame jFrame) {
     jFrame.dispose();
}

I just thought it would be better if I disposed my JFrame on the event dispatch thread. 我只是认为如果将JFrame放在事件分发线程上会更好。

Why does the returned array of Frame.getFrames() still contain my JFrames ? 为什么返回的数组Frame.getFrames()还包含我JFrames

Java uses a garbage collector. Java使用垃圾回收器。 Disposing the frame only makes it ready for garbage collection, thus those JFrames exist until the GC comes in and deletes them. 处置框架只会使其准备好进行垃圾收集,因此这些JFrame存在,直到GC进入并删除它们为止。

JFrame.dispose() only frees up the resources . JFrame.dispose()仅释放资源

In Java, objects are references pointing to a group of resources. 在Java中,对象是指向一组资源的引用。 So if the memory is cleared, the reference is still left over. 因此,如果清除了内存,则仍保留引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM