2011-12-21 7 views
8

C'è qualche differenza tra i due. Stavo leggendo un articolo (http://www.javalobby.org/java/forums/t17933) su che si dovrebbe sempre usareSystem.exit (0) vs JFrame.EXIT_ON_CLOSE

System.exit(0); 

Attualmente io uso

JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

L'articolo dice che anche per uno swing applicazione Java si dovrebbe aggiungere un ascoltatore WindowAdapter ed e call System.exit() all'interno del suo metodo windowClosing(WindowEvent e).

C'è qualche differenza? Un metodo è migliore dell'altro?

risposta

12

Se si guarda il codice JFrame, lo fa:

protected void processWindowEvent(WindowEvent e) { 
     super.processWindowEvent(e); 

     if (e.getID() == WindowEvent.WINDOW_CLOSING) { 
      switch(defaultCloseOperation) { 
       ... 
      case EXIT_ON_CLOSE: 
        // This needs to match the checkExit call in 
        // setDefaultCloseOperation 
     System.exit(0); 
     break; 
      } 
     } 
    } 

Quindi, è esattamente la stessa cosa. Vorrei semplicemente impostare EXIT_ON_CLOSE se è quello che vuoi che faccia.

0

Bene, considerando che System.exit (0) è nella codifica JFrame, potrebbe funzionare.