Frage

Given the following code:

public static void main(String[] args) 
{
    SwingUtilities.invokeLater(new Runnable() 
    {
        public void run() 
         {
            ClientGUI gui = new ClientGUI();
            gui.start();
         }
    });
}

everything works fine, I get a nice GUI window. OK. Now, lets add an infinite loop after gui.start() :

public static void main(String[] args) 
{
    SwingUtilities.invokeLater(new Runnable() 
    {
        public void run() 
         {
            ClientGUI gui = new ClientGUI();
            gui.start();
            while (true) {
            }
         }
    });
}

and the output is a blank window that does not react to window exiting. Can someone explain me what exactly happans here?

War es hilfreich?

Lösung

Can someone explane me what exactly happans here?

Sure. You're keeping the event dispatch thread tied up in an infinite loop, so it never gets to react to events such as "close window". Don't do that.

You should keep the event dispatch thread available for as much of the time as possible - don't perform any long-running tasks on it, including IO operations such as reading from files or the network.

See the "Concurrency in Swing" tutorial for more details.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top