0

我正在调用一个线程,在该线程中我再次调用同一个类

TrafficMainGUI traffic=new TrafficMainGUI(storeValue);
traffic.setVisible(true);

但我希望前一个类对象被破坏。我怎样才能做到这一点。

由于 TrafficMainGUI 是一个 jFrame 对象。请帮忙??

4

3 回答 3

2

要正确销毁 a JFrame,您应该处置它。

previousTraffic.dispose();
TrafficMainGUI traffic=new TrafficMainGUI(storeValue);
traffic.setVisible(true);

从文档中:

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.

您的问题对于您使用线程所做的事情非常模糊。正如@MadProgrammer 所提到的,当您使用 时swing,您应该考虑到EDT。但要获得更具体的帮助,您应该提供sscce

于 2013-03-18T08:48:54.930 回答
0

To make your frame disappear just call

traffic.setVisible(true);

This however does not remove the instance of TrafficMainGUI you created. Since java has automatic garbage collection this object will be removed at some point of time automatically when all references that refer to it are not accessible. For example if your variable traffic is defined in method scope it becomes obsolete once your code exits the method. If not you can say traffic = null;. This will remove the reference.

You should note however that GC (garbage collector) lives its own life and can decide itself when to remove your object. It can decide not to remove it even forever. But you should not care about it.

于 2013-03-18T08:51:22.060 回答
0

添加此代码:

traffic = new TrafficMainGUI(newValues);

流量将由新对象分配,并且先前的对象将被替换,因为new函数是请求内存中的新对象。

于 2013-03-18T08:48:22.533 回答