Java 如何删除对话框引用的对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2445365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:47:01  来源:igfitidea点击:

How do I delete an object referenced by a dialog?

javaswing

提问by joseph

I created a dialog with a jpanel inside it, and that jpanel will be still referenced if I get rid of the dialog. I want to destroy that dialog and everything in it when I click a cancel button. How do I delete both the dialog and the jpanel?

我创建了一个对话框,里面有一个 jpanel,如果我摆脱了这个对话框,那个 jpanel 仍然会被引用。当我单击取消按钮时,我想销毁该对话框及其中的所有内容。如何删除对话框和 jpanel?

采纳答案by David Sykes

Answering the question(s) you posed in the comment:

回答您在评论中提出的问题:

Once you have displayed a dialog:

显示对话框后:

setVisible(true);

you hide it by calling:

你通过调用隐藏它:

setVisible(false);

and then you must call:

然后你必须打电话:

dialog.dispose();

to make sure that all native GUI resources the dialog used get freed. Once you have done this, the garbage collector will clean up all of the objects once you no longer have any references to them.

确保对话框使用的所有本机 GUI 资源都被释放。完成此操作后,垃圾收集器将在您不再拥有对它们的任何引用时清除所有对象。

回答by Raul Agrait

No need to delete the object. The garbage collector will take care of the memory as soon as it is no longer referenced.

无需删除对象。一旦内存不再被引用,垃圾收集器就会处理它。

回答by Ashish Gupta

You meant "how" to destroy it? There is no way to destroy an object explicitly in Java. garbage collector in Java automatically reclaims the memory occupied by it If there is no reference of the same exists.

你的意思是“如何”摧毁它?在 Java 中无法显式销毁对象。Java中的垃圾收集器会自动回收它所占用的内存,如果没有相同的引用存在。

"but I create dialog with jpanel inside it, and that jpanel will be still referrenced. I want to destroy that dialog when click my own button "Cancel"

“但我在其中创建了带有 jpanel 的对话框,并且该 jpanel 仍将被引用。我想在单击我自己的按钮“取消”时销毁该对话框

Just try setting that JPanel object to null or call the dispose method on it if that is available.

只需尝试将该 JPanel 对象设置为 null 或调用它的 dispose 方法(如果可用)。

回答by GuruKulki

If you want to delete the assign that object reference to null, so that when Garbage Collector runs for the next time it can destroy the object thinking it is not getting referenced.

如果您想删除将该对象引用分配给 null,以便在垃圾收集器下次运行时它可以销毁该对象,认为它没有被引用。

回答by Itay Maman

You can override the finalize() method (see http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#finalize%28%29) to perform cleanup when an object is destroyed.

您可以覆盖 finalize() 方法(请参阅http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#finalize%28%29)以在对象被摧毁。

However, unlike C++ there is no guarantee when will this method get called. In C++ you have stack-stored objects which are destroyed when execution leaves the scope in which they were defined.

但是,与 C++ 不同,无法保证何时调用此方法。在 C++ 中,您有堆栈存储的对象,当执行离开它们定义的范围时,这些对象会被销毁。

In Java all object are stored on the heap. They will be finalized when the Garbage collector decides to collect them (implies that they are not reachable from your app) but you don't know when will the GC kick in. Thus, if you have some cleanup that must take place at a certain point (e.g., closing a file so that it can be written to) you have to code it yourself and not rely on the finalize() method being called.

在 Java 中,所有对象都存储在堆上。当垃圾收集器决定收集它们(意味着它们无法从您的应用程序访问它们)时,它们将被最终确定,但您不知道 GC 何时开始。因此,如果您必须在某个时间进行一些清理点(例如,关闭文件以便可以写入)您必须自己编写代码,而不是依赖于被调用的 finalize() 方法。

The typical pattern for doing that is based on a try ... finallyblock:

这样做的典型模式基于一个try ... finally块:

X x = null;
try {
  // ... do some stuff
  x = ... // obtain an object
   ... // do some stuff 
}
finally {
  if(x != null)
     x.close(); // Perform cleanup WRT x
}

(Admittedly, ugly)

(不可否认,丑)

回答by Artic

There is no need to destroy object in Java in the way like in C++.There is garbage collector which destroys(release memory used by) objects automatically after there is no references to this object in running code. Everything that you can do is to force destroy link by Object obj = null;This kills reference to obj.

Java 中不需要像 C++ 那样销毁对象。有垃圾收集器,在运行代码中没有对该对象的引用后,它会自动销毁(释放所使用的内存)对象。您可以做的一切就是通过Object obj = null;这会杀死对 obj 的引用来强制销毁链接。

回答by Lalith J.

  1. If it is a Window and if it is visible

    I. frame.setVisible(false);

    II. frame.dispose();

    II. set reference to null (ex.. frame=null;)

  2. If it is not a Window

    I.set reference to null (ex.. x=null;)

  1. 如果它是一个窗口并且它是可见的

    I.frame.setVisible(false);

    二、frame.dispose();

    二、将引用设置为 null(例如。frame=null;)

  2. 如果它不是一个窗口

    I.set 对 null 的引用(例如.. x=null;)

That is all , once object be free GC will free the resources when it is like.

就是这样,一旦对象被释放,GC 就会像这样释放资源。

Here are somethings you must understand

这里有一些你必须明白的

*. You as Java programmer can not force Garbage collection in Java;it will only trigger if JVM thinks it needs a garbage collection based on Java heap size.

*. 作为 Java 程序员的您不能在 Java 中强制进行垃圾回收;它只会在 JVM 认为它需要基于 Java 堆大小的垃圾收集时触发。

*. There are methods like System.gc ()and Runtime.gc ()which is used to send request of Garbage collection to JVM but it's not guaranteed that garbage collection will happen.

*. 有System.gc()Runtime.gc() 等方法用于向 JVM 发送垃圾收集请求,但不保证垃圾收集会发生。

*. If there is no memory space for creating new object in Heap Java Virtual Machine throws OutOfMemoryError or java.lang.OutOfMemoryError heap space

*. 如果堆中没有用于创建新对象的内存空间 Java 虚拟机抛出 OutOfMemoryError 或 java.lang.OutOfMemoryError 堆空间

And search about this...

并搜索这个...

J2SE 5(Java 2 Standard Edition) adds a new feature called Ergonomics. goal of ergonomics is to provide good performance from the JVM with minimum of command line tuning.

J2SE 5(Java 2 标准版)添加了一项名为Ergonomics的新功能。人体工程学的目标是通过最少的命令行调整从 JVM 提供良好的性能。

回答by Aditya Singh

"System.gc()" is the best way.

“System.gc()”是最好的方法。

gc-Garbage Collector.

gc-垃圾收集器。

This is how you are gonna destroy the object in the program itself

这就是你将如何销毁程序本身中的对象

Sample Code:

示例代码:

public class TestRun
{
  public static void main(String args[])
  {
     /*1*/     TestRun tr=new TestRun(); 
     /*2*/     System.gc(); //You can omit this step. This is just an example. 
     /*3*/     tr=null;    
     /*4*/     System.gc();  
  }    
}

Explanation

解释

  1. An object of class TestRun is created and its reference is stored in the variable 'tr'.

  2. Garbage Collector is called. But makes no effect. Because no object is dereferenced yet.

  3. The object that was created in step-1, is now dereferenced.But the space that was occupied by the object is still blocked by it.

  4. Garbage Collector is again invoked, and now it sees that the object that was created in step-1 is now dereferenced. Hence, now it frees the space occupied by the object and the object is now deleted from the memory in simple language.

  1. 创建了一个 TestRun 类的对象,并将其引用存储在变量“tr”中。

  2. 垃圾收集器被调用。但是没有效果。因为还没有对象被解除引用。

  3. 在步骤 1 中创建的对象现在被取消引用。但对象占用的空间仍然被它阻塞。

  4. 垃圾收集器再次被调用,现在它看到在步骤 1 中创建的对象现在被取消引用。因此,现在它释放了对象占用的空间,并且对象现在以简单的语言从内存中删除。

In fact it will delete all those objects that are already dereferenced.

事实上,它会删除所有已经解除引用的对象。

It's good practise to keep calling it in your code.

在您的代码中继续调用它是一种很好的做法。