JFrame 被处置后的价值是多少?

What is the value of a JFrame after it has been disposed?

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

JFrame 是一个对象,因此永远不能为 null,而 JFrame 变量可以引用有效的 JFrame 对象或没有引用,因此 "be null"。如果您在 JFrame 对象上调用 dispose(),该对象仍然存在,因此该变量不为空。然而,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).