PDA

View Full Version : Come fermare un programma in java?


infomo
10-08-2006, 16:58
Ciao a tutti e buon proseguimento d'estate,

sto facendo un programma in java e ho un assoluto bisogno di far fermare il programma che sto costruendo finchè non si verifica una condizione.

In pratica sto facendo un pò di grafica e voglio che il mio programma fermi l'esecuzione finchè non è premuto un bottone nel pannello...

spero di essermi spiegato bene, per favore aiutatemi è urgente, GRAZIE

cj++
10-08-2006, 17:01
potresti gestire la cosa tramite thread... e tenerlo bloccato finche appunto finchè nn arriva l'evento.

franksisca
10-08-2006, 17:16
oppure metti una azione nel pulsante, ovvero quando premi fai esci;)

in codice
pulsanteCheFaAzione.addActionerListener(new Action Listener(){
public void actionPerformed(actionEvent e){
System.exit(-1);
}
}
);

infomo
10-08-2006, 17:18
Ma cosa comporta la chiamata System.exit quando all'interno gli passo -1 o zero?

franksisca
10-08-2006, 17:21
Ma cosa comporta la chiamata System.exit quando all'interno gli passo -1 o zero?
c'era una differenza, ma sincerametne non ricordo quale, comunque credo che a livello "utente" non cambi nulla ;)

franksisca
10-08-2006, 17:46
dalle api di java exit

public void exit(int status)

Terminates the currently running Java virtual machine by initiating its shutdown sequence. This method never returns normally. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

The virtual machine's shutdown sequence consists of two phases. In the first phase all registered shutdown hooks, if any, are started in some unspecified order and allowed to run concurrently until they finish. In the second phase all uninvoked finalizers are run if finalization-on-exit has been enabled. Once this is done the virtual machine halts.

If this method is invoked after the virtual machine has begun its shutdown sequence then if shutdown hooks are being run this method will block indefinitely. If shutdown hooks have already been run and on-exit finalization has been enabled then this method halts the virtual machine with the given status code if the status is nonzero; otherwise, it blocks indefinitely.

The System.exit method is the conventional and convenient means of invoking this method.

Parameters:
status - Termination status. By convention, a nonzero status code indicates abnormal termination.
Throws:
SecurityException - If a security manager is present and its checkExit method does not permit exiting with the specified status
See Also:
SecurityException, SecurityManager.checkExit(int), addShutdownHook(java.lang.Thread), removeShutdownHook(java.lang.Thread), runFinalizersOnExit(boolean), halt(int)



quindi il -1 mio è sbagliato in realtà ;)