Answer the question
In order to leave comments, you need to log in
How to destroy Window(JFrame)?
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 Window ever created (including a reference to the window being destroyed). How then to destroy 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();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question