java - JFrame 被释放后的值(value)是多少?

标签 java swing jframe dispose

该值不一定为 null(我认为?),因为它仍然是有效的引用。

最佳答案

JFrame 是一个对象,因此永远不能为 null,而 JFrame 变量可以引用有效的 JFrame 对象,也可以没有引用,因此“为 null”。如果您对 JFrame 对象调用 dispose(),该对象仍然存在,因此该变量不为 null。然而,JFrame 释放了一些系统资源。如果重新渲染 JFrame 对象,将再次获得这些资源。

通常情况下,解决此类问题的最佳测试是 1) 创建并运行测试代码,2) 检查 API

例如,

JFrame frame = new JFrame("Foo");
frame.pack();
frame.setVisible(true);

// in some listener
frame.dispose(); // it's no longer visible
System.out.println("is frame null? " + (frame == null));

根据 Window API,其中 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. 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).

关于java - JFrame 被释放后的值(value)是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38505068/

相关文章:

java - Spring 数据 JPA : How to execute aggregate functions

java - ActionPerformed 在 Java 中运行不佳

java - Android 多线程应用程序崩溃

java - 异步任务的问题

java - 使用 Swing 在 JFrame 上设置背景图像

java - 绘图程序重复 JMenuBar

Java - 子类中的绘制方法不起作用

java - Swing 不遵守我的 GridLayout Rows x Columns 定义。怎么修?

java - JTextPane 中的 Swingx 搜索

java - 为什么在 Java 中最小化、最大化和调整大小时更改 JFrame 中的高度和宽度值