2013-03-18 57 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(垃圾回收器)的生活,並可以決定何時移除您的對象。它可以決定不會永遠刪除它。但你不應該關心它。