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

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


但是我希望上一个类对象被破坏。我如何才能做到这一点。

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

最佳答案

要正确销毁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

关于java - 销毁当前类对象并通过线程运行同一类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15472616/

10-10 23:59