2013-03-18 49 views
0

我打电话一个线程中,我又打电话同一类销毁当前类对象,并通过螺纹

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

运行同一类,但我想以前的类对象来获取destroy.How我能达致这。

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

+1

“摧毁”意味着什么?关闭框架? – Kai 2013-03-18 08:44:13

+1

我希望你不要在事件派发线程之外创建/修改或者与UI组件交互 – MadProgrammer 2013-03-18 08:47:31

+1

更多细节可以在这里找到(http://stackoverflow.com/a/6310284/230513)。 – trashgod 2013-03-18 11:25:07

回答

0

添加以下代码:

traffic = new TrafficMainGUI(newValues); 

流量将由新的对象被分配,和先前的对象将被替换为new功能是在存储器请求新的对象。

+1

假设没有其他对象维持对前一个值 – MadProgrammer 2013-03-18 08:50:16

+0

的强烈引用是的,假设你确定你在做什么.. – 2013-03-18 08:52:57

2

要正确销毁一个JFrame,你应该是dispose吧。

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

0

为了使您的框消失就叫

traffic.setVisible(true); 

然而,这不会删除您创建的TrafficMainGUI实例。由于java具有自动垃圾收集功能,因此当所有引用它的引用都不可访问时,该对象将在某个时间点自动删除。例如,如果变量traffic在方法范围中定义,那么一旦代码退出该方法,该变量就会过时。如果不是,你可以说traffic = null;。这将删除参考。

但是,您应该注意GC(垃圾回收器)的生活,并可以决定何时移除您的对象。它可以决定不会永远删除它。但你不应该关心它。