The Shutdown event for forms and dialogs in Java swing is nothing more than the windowclosing and windowclosed methods, which are needed if you want to prompt for "exit" when you close or save data. But in the end is to use which method has certain fastidious.
Windowclosing is called when the form is closed, and the form is still running and displayed, so it is convenient to call this method the method before the shutdown. It is important to note that this method is invoked when we press the Close button in the upper-right corner of the form or dialog box.
WindowClosed is invoked when a form is closed, which is typically called when we close a form or dialog box in code.
The method associated with closing a form or dialog box is Dispose, which releases the associated form after being invoked and releases the appropriate resource, and then invokes the WindowClosed method. Of course, you can also use the System.exit (0) method to exit the program directly to achieve the effect of closing the form, but in this case the program will not invoke the WindowClosed event. In addition, according to the names of these two events will give people a misunderstanding, that the form is closed will first call windowclosing, and then call windowclosed, in fact, if you use Dispose to close the form will not call the Windowclosing method.
Ok. I wrote a program to illustrate the two method invocation relationships
Import java.awt.Dimension;
Import Java.awt.FlowLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JOptionPane;
Import Javax.swing.JPanel;
public class Closeeventdemo extends JFrame {public static void main (string[] args) {new Closeeventdemo ();
Public Closeeventdemo () {//Create a panel JPanel pacontent = new JPanel (new FlowLayout (Flowlayout.center));
Pacontent.setpreferredsize (New Dimension (400, 100));
Create a button in the panel to close the form JButton btn = new JButton ("Off");
Btn.setpreferredsize (New Dimension (100, 25));
Btn.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {closeframe ();
}
});
Load the button into the panel pacontent.add (BTN);
Setup Panel This.settitle ("Close Event Demo");
This.setdefaultcloseoperation (Jframe.do_nothing_on_close); This.setcontentpane (PAContent);
This.pack ();
This.setlocationrelativeto (NULL);
This.setvisible (TRUE); This.addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) {System.out.println ("trigger
Windowclosing event ");
Closeframe ();
public void windowclosed (WindowEvent e) {System.out.println ("Trigger windowclosed event");
}
});
//close form private void Closeframe () {System.out.println ("invoke form close function"); int result = Joptionpane.showconfirmdialog (null, "whether to exit.)
"," Exit confirmation ", joptionpane.yes_no_option, Joptionpane.question_message);
if (result = = Joptionpane.yes_option) this.dispose ();
}
}