简体   繁体   English

如何完全关闭/删除JFrame

[英]How to completely close/delete A JFrame

I made a JFrame (call it J1 ) that includes a JButton. 我制作了一个包含JButton的JFrame(称为J1 )。 When the button is clicked, a new JFrame (call it J2 ) opens. 单击该按钮时,将打开一个新的JFrame(称为J2 )。 J1 also has an ArrayList that contains J2 , and all other Jframes that were opened. J1还具有一个ArrayList,其中包含J2以及所有其他打开的Jframe。 J2 is set to DISPOSE_ON_CLOSED I set for J2 a windowClosed() method. J2设置为DISPOSE_ON_CLOSED我为J2设置了一个windowClosed()方法。 For testing, inside the JFrame I looped threw the ArrayList , until I get to the current J2 that was just closed, and surprisingly when I do J2.setVisisble(true) the J2 window returns! 为了进行测试,我在JFrame中循环抛出ArrayList ,直到到达刚刚关闭的当前J2为止,令人惊讶的是,当我执行J2.setVisisble(true)J2窗口返回了!

I also checked threw the Task Manager, and saw the though opening a new J2 , will make the overall program consume more RAM, closing each J2 doesn't show much difference on the Task Manager, it doesn't look like any memory was freed. 我还检查了任务管理器的运行情况,并看到尽管打开了一个新的J2会使整个程序消耗更多的RAM,但关闭每个J2在任务管理器上显示的差异并不大,看起来好像没有释放任何内存。 It looks like that the memory consumption is going back to "normal" after a few seconds, so i doubt that is has anything to do directly with the J2 . 几秒钟后,内存消耗似乎恢复到“正常”状态,因此我怀疑这与J2有直接关系。

I tried printing ( System.out.print ) all the ArrayList content every time a new J2 is initiated, and after opening a window, closing it, and opening a new one, I get the following messgae: 每次启动新的J2 ,我都尝试打印( System.out.print )所有ArrayList内容,并且在打开一个窗口,将其关闭并打开一个新窗口之后,得到以下消息:

home.ATMmachine[frame0,252,198,620x420,invalid,hidden,layout=java.awt.BorderLayout,title=ATM Machine No.1,resizable,normal,defaultCloseOperation=DISPOSE_ON_CLOSE,rootPane=javax.swing.JRootPane[,9,38,602x373,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]

This means that the J2 wasn't completely deleted! 这意味着J2尚未完全删除!

The JFrame dispose() method is suppose to JFrame dispose()方法应该

Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. 释放此Window及其子组件及其所有子级使用的所有本机屏幕资源。 That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable. 也就是说,这些组件的资源将被销毁,它们消耗的任何内存将返回给OS,并将它们标记为不可显示。

So am I just wrong, and the frame is being deleted sum time later. 所以我是错的,并且在相加时间之后删除了该帧。

If not, then how can i delete the frame in such a way that referencing to it would be like referencing to null? 如果不是,那么我如何以引用该框架就像引用为null的方式删除该框架?

I made a JFrame (call it J1) that includes a JButton. 我制作了一个包含JButton的JFrame(称为J1)。 When the button is clicked, a new JFrame (call it J2) opens. 单击该按钮时,将打开一个新的JFrame(称为J2)。 J1 also has an ArrayList that contains J2, and all other Jframes that were opened. J1还有一个包含J2和所有其他打开的Jframe的ArrayList。 J2 is set to DISPOSE_ON_CLOSED I set for J2 a windowClosed() method. J2设置为DISPOSE_ON_CLOSED我为J2设置了一个windowClosed()方法。 For testing, inside the JFrame I looped threw the ArrayList, until I get to the current J2 that was just closed, and surprisingly when I do J2.setVisisble(true) the J2 window returns! 为了进行测试,我在JFrame内循环抛出ArrayList,直到到达刚刚关闭的当前J2,而且令人惊讶的是,当我执行J2.setVisisble(true)时,J2窗口返回了!

Closing the JFrame and disposing does not destroy the JFrame object, but rather it releases system resources, the resources needed to display the window on your OS. 关闭JFrame并进行处理不会破坏JFrame对象,而是释放系统资源,即在OS上显示窗口所需的资源。 This is entirely different from the object itself. 这与对象本身完全不同。 When you call setVisible(true) on a disposed JFrame, the resources are re-created, and the window is re-displayed. 当在已处置的JFrame上调用setVisible(true)时,将重新创建资源,并重新显示窗口。 No objects are created or destroyed by this. 这样就不会创建或销毁任何对象。

I also checked threw the Task Manager, and saw the though opening a new J2, will make the overall program consume more RAM, closing each J2 doesn't show much difference on the Task Manager, it doesn't look like any memory was freed. 我还检查了任务管理器,并看到虽然打开了一个新的J2,将使整个程序消耗更多的RAM,关闭每个J2在任务管理器上没有太大的区别,看起来好像没有释放任何内存。 It looks like that the memory consumption is going back to "normal" after a few seconds, so i doubt that is has anything to do directly with the J2. 几秒钟后,内存消耗似乎恢复到“正常”状态,因此我怀疑这与J2有直接关系。

No surprise there. 毫不奇怪。

I tried printing (System.out.print) all the ArrayList content every time a new J2 is initiated, and after opening a window, closing it, and opening a new one, I get the following messgae: 每次启动新的J2时,我都尝试打印(Array.out.print)所有ArrayList内容,并且在打开一个窗口,将其关闭并打开一个新窗口之后,得到以下消息:

home.ATMmachine[frame0,252,198,620x420,invalid,hidden,layout=java.awt.BorderLayout,title=ATM Machine No.1,resizable,normal,defaultCloseOperation=DISPOSE_ON_CLOSE,rootPane=javax.swing.JRootPane[,9,38,602x373,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true] home.ATMmachine [frame0,252,198,620x420,invalid,hidden,layout = java.awt.BorderLayout,title = ATM Machine No.1,可调整大小,正常,defaultCloseOperation = DISPOSE_ON_CLOSE,rootPane = javax.swing.JRootPane [,9,38,602x373 ,invalid,layout = javax.swing.JRootPane $ RootLayout,alignmentX = 0.0,alignmentY = 0.0,border =,flags = 16777673,maximumSize =,minimumSize =,preferredSize =],rootPaneCheckingEnabled = true]

This means that the J2 wasn't completely deleted! 这意味着J2尚未完全删除!

Again, you're confusing objects, which will exist as long as a reference to the object exists, and resources. 同样,您混淆了对象(只要存在对对象的引用)和资源,这些对象将一直存在。

The JFrame dispose() method is suppose to Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. JFrame dispose()方法用于释放此Window,其子组件及其所有子级使用的所有本机屏幕资源。 That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable. 也就是说,这些组件的资源将被销毁,它们消耗的任何内存都将返回给OS,并将它们标记为不可显示。

Exactly. 究竟。

So am I just wrong, and the frame is being deleted sum time later. 所以我是错的,并且在相加时间之后删除了该帧。

An object can only be GC'd when there are no more strong references to it, and the garbage collector only does this when it feels the need to do so, such as when memory is about to run out. 仅当不再有对它的强引用时,才可以对对象进行GC,而垃圾收集器仅在感觉需要这样做时(例如,内存快要用完时)才这样做。

If not, then how can i delete the frame in such a way that referencing to it would be like referencing to null? 如果不是,那么我如何以引用该框架就像引用为null的方式删除该框架?

Just like any other object. 就像其他任何对象一样。 You could create it locally for one, but be careful since Swing listeners have a way to create strong references that you would assume should be weak, and thus make objects persist for longer than you'd like. 您可以在本地创建它,但是要小心,因为Swing侦听器有一种方法可以创建强引用,而您认为该强引用应该是弱的,从而使对象的持久保存时间超出您的期望。


On a side note -- why all the JFrames? 附带说明一下-为什么要使用所有JFrame? Most programs I've used that throw a lot of windows at you seem to be quite annoying, which I guess is why I usually only see them created in this way by newbie programmers. 我使用的大多数程序会给您带来很多麻烦,这似乎很烦人,我想这就是为什么我通常只看到新手程序员以这种方式创建它们的原因。 Why not swap views instead with a CardLayout? 为什么不使用CardLayout交换视图呢?

Please check out: The Use of Multiple JFrames, Good/Bad Practice? 请检出: 使用多个JFrame,良好/不良做法? for more on this 有关此的更多信息

指针j2仍然在ArrayList中引用帧j2,因此垃圾回收器还不能处理它...尝试从arraylist中删除指针,即使j2仍在RAM中,它也没有任何引用在您的程序中,因此垃圾收集器应在下一次通过时,或者在您调用system.gc()时将其删除。

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

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