Re: Query:the real function of System.exit(0)?
Jack Dowson wrote:
...
As we all know,System.exit(0) is often used to quit the program.
But when it applied to some of my programs,the outcome was not so.
I created a frame using class JFrame.Then I registerd WindowsAdapter
That is most probably the problem. If you spelt the
method name incorrectly, or using wrong arguments
using a WindowAdapter gives no complaint at compile
time, and no effect at runtime. Try implementing a
WindowListener instead - that forces you to implement
the correct methods.
See if this example works for you..
<sscce>
import java.awt.event.*;
//import java.awt.Frame;
import javax.swing.JFrame;
class CloseWindow {
public static void main(String[] args) {
//Frame f = new Frame("Close Me!");
JFrame f = new JFrame("Close Me!");
f.addWindowListener( new WindowListener(){
public void windowDeactivated(WindowEvent we) {}
public void windowActivated(WindowEvent we) {}
public void windowDeiconified(WindowEvent we) {}
public void windowIconified(WindowEvent we) {}
public void windowOpened(WindowEvent we) {}
public void windowClosed(WindowEvent we) {}
public void windowClosing(WindowEvent we) {
System.out.println(we);
System.exit(0);
}
} );
f.setSize(200,100);
f.setLocation(50,50);
f.setVisible(true);
}
}
</sscce>
Note that for Swing based JFrames, I would heed
Daniel's advice to use defaultCloseOperation(),
but adding a WindowListener works, and does
so for both Swing and AWT based components.
In fact, uncomment the two commented lines,
and comment the two lines following them, and
you might see the same effect on an AWT Frame.
HTH
--
Andrew Thompson
http://www.athompson.info/andrew/
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200705/1