简体   繁体   English

JFrame处理后的值是什么?

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

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

A JFrame is an object and so can never be null, while a JFrame variable can either refer to a valid JFrame object or have no reference and thus "be null". JFrame是一个对象,因此永远不能为null,而JFrame变量可以引用有效的JFrame对象,也可以没有引用,因此为“ null”。 If you call dispose() on the JFrame object the object still exists and so the variable is not null. 如果在JFrame对象上调用dispose() ,则该对象仍然存在,因此变量不为null。 The JFrame however has released some system resources. 但是,JFrame已释放了一些系统资源。 These resources will be gained again if the JFrame object is re-rendered. 如果重新渲染JFrame对象,将再次获得这些资源。

As is often the case, the best test for a question like this is to 1) create and run test code and 2) check the API 通常,针对此类问题的最佳测试是:1)创建并运行测试代码,以及2)检查API

eg, 例如,

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));

Per the Window API, where the dispose method is inherited from: 根据Window API,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,并将它们标记为不可显示。 The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. 通过重建本机资源以及随后的打包或显示调用,可以再次显示Window及其子组件。 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). 重新创建的Window及其子组件的状态与放置Window时这些对象的状态相同(不考虑这些动作之间的其他修改)。

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

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