There is a window, when you click on the button, the window should close and be destroyed. How to do this, knowing that the Window.getOwnerlessWindows() method returns references to all Windows ever created (including a link to the window being deleted). How then to destroy the Window ?

Here is an example:

 package javaapplication28; import java.awt.Window; import javax.swing.JFrame; public class JavaApplication28 { public static void main(String[] args) { // final JFrame frame = new JFrame("TEST"); frame.pack(); frame.setVisible(true); // new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException ex) {} //袣袪校袚 1, 校袟袧袗袝袦 袧袗楔袠 肖袪袝袡袦蝎 Window[] wind = Window.getOwnerlessWindows(); int a = 0; int max = wind.length; System.out.println("小褋褘谢泻懈 薪邪 褎褉械泄屑褘 写芯 褍薪懈褔褌芯卸械薪懈褟"); while(a!=max){ System.out.println(wind[a].getClass().getCanonicalName()); ((JFrame)wind[a]).setTitle(((JFrame)wind[a]).getTitle().concat(" 袣袪校袚 1")); a = a + 1; } // //袩蝎孝袗袝袦小携 校袘袠孝鞋 frame.dispose(); //校袘袠袥袠 袨袣袧袨 System.gc(); //袧袗 袙小携袣袠袡 // try { Thread.sleep(1000); } catch (InterruptedException ex) { } //袣袪校袚 2, 袩袨袥校效袗袝袦 小小蝎袥袣校 袧袗 肖袪袝袡袦 袣袨孝袨袪蝎袡 校袘袠孝, 袠 袩袨袣袗袟蝎袙袗袝袦 袝袚袨 肖袪袝袡袦) wind = Window.getOwnerlessWindows(); a = 0; max = wind.length; System.out.println("小褋褘谢泻懈 薪邪 褎褉械泄屑褘 锌芯褋谢械 褍薪懈褔褌芯卸械薪懈褟"); while(a!=max){ System.out.println(wind[a].getClass().getCanonicalName()); ((JFrame)wind[a]).setTitle(((JFrame)wind[a]).getTitle().concat("袣袪校袚 2")); wind[a].setVisible(true); a = a + 1; } //袙 袠孝袨袚袝 袛袨袥袞袧袨 袩袨袥校效袠孝小携 TEST, 袣袪校袚1, 袣袪校袚2 袙 TITLE (懈 屑褘 蟹薪邪械屑 褋泻芯谢褜泻芯 薪邪褕 褎褉械泄屑 锌褉芯褕械谢 泻褉褍谐芯胁)胁 胁懈写懈屑芯屑 褍斜懈褌芯屑 芯泻薪械) } }).start(); } } 
  • The last sentence is unclear. Can you reformulate? - LEQADA
  • 6
    @ Denis Kotlyarov Destroy him morally! - Vlad from Moscow
  • You need to put a listener and call the dispose () method. And about the links did not understand. - LEQADA
  • @VladfromMoscow ahaha for sure. The only way - dirkgntly
  • one
    dispose does not kill the window. what it does to it exactly is written in javadoc. And the link to the window is kept in the main: final JFrame frame method - it does not go anywhere and cg is not cleaned until the very end of the program's work. Alter the program so that it does not keep references to the windows. Probably so. - Sergey

1 answer 1

 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 

if you need to close the application immediately

 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

but gc will not clear it. there is a link to this object, it is necessary to untie it from the variable

 frame = null;