gpt4 book ai didi

java - 如何完全关闭/删除 JFrame

转载 作者:行者123 更新时间:2023-11-29 07:45:39 25 4
gpt4 key购买 nike

我制作了一个包含 JButton 的 JFrame(称之为 J1)。单击该按钮时,将打开一个新的 JFrame(将其命名为 J2)。J1 还有一个 ArrayList,其中包含 J2 和所有其他已打开的 Jframes。J2 设置为 DISPOSE_ON_CLOSED我为 J2 设置了一个 windowClosed() 方法。为了测试,我在 JFrame 中循环抛出 ArrayList,直到我到达刚刚关闭的当前 J2,令人惊讶的是当我执行 J2.setVisisble( true) J2 窗口返回!

我也检查了任务管理器,看到虽然打开一个新的J2,会使整个程序消耗更多的RAM,关闭每个J2不显示任务管理器上的差异很大,看起来没有释放任何内存。看起来内存消耗在几秒钟后恢复到“正常”,所以我怀疑这与 J2 有直接关系。

每次启动新的 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]

这意味着 J2 没有完全删除!

假设 JFrame dispose() 方法

Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. 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.

所以我是不是错了,帧稍后会被删除。

如果不是,那么我怎样才能以引用它就像引用 null 的方式删除框架?

最佳答案

I made a JFrame (call it J1) that includes a JButton. When the button is clicked, a new JFrame (call it J2) opens. J1 also has an ArrayList that contains J2, and all other Jframes that were opened. J2 is set to DISPOSE_ON_CLOSED I set for J2 a windowClosed() method. 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 并进行处置不会破坏 JFrame 对象,而是会释放系统资源,即在操作系统上显示窗口所需的资源。这与对象本身完全不同。当您对已处置的 JFrame 调用 setVisible(true) 时,会重新创建资源,并重新显示窗口。这不会创建或销毁任何对象。

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. 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.

不足为奇。

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:

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!

同样,您混淆了对象和资源。

The JFrame dispose() method is suppose toReleases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. 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.

没错。

So am I just wrong, and the frame is being deleted sum time later.

一个对象只有在没有强引用时才会被 GC,垃圾收集器只有在它觉得有必要这样做时才这样做,比如当内存即将耗尽时。

If not, then how can i delete the frame in such a way that referencing to it would be like referencing to null?

就像任何其他对象一样。您可以在本地为一个对象创建它,但要小心,因为 Swing 监听器有一种方法可以创建您认为应该是弱引用的强引用,从而使对象持续的时间比您希望的要长。


旁注——为什么要使用所有 JFrames?我用过的大多数程序都会向您抛出很多窗口,这似乎很烦人,我想这就是为什么我通常只看到新手程序员以这种方式创建它们的原因。为什么不用 CardLayout 来交换 View ?

请查看:The Use of Multiple JFrames, Good/Bad Practice?了解更多

关于java - 如何完全关闭/删除 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26108815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com