Frame.addwindowlistener (New Windowadapter () {
@Override
public void windowclosing (WindowEvent e) {
int choose = Joptionpane.showconfirmdialog (null, "Are you sure you want to exit?") ",
"Message hint", joptionpane.yes_no_option);
if (choose = = Joptionpane.yes_option) {
System.exit (0);
} else if (choose = = Joptionpane.no_option) {
return;//here is missing JFrame, but the program does not exit. It should just be hidden. I print out the Visible property is True
}
}
});
Choose has 3 values, in addition to the above two, there are users directly click the Close dialog box button
JFrame How to close a window
public void setdefaultcloseoperation (int operation)
Operation Default is Hide_on_close
So to use WindowListener, you must set the operation to
Do_nothing_on_close
Reference API:
setdefaultcloseoperationpublic void setdefaultcloseoperation (int operation)
Sets the action that the user performs by default when "Close" is initiated on this form. You must specify one of the following options:
Do_nothing_on_close (defined in windowconstants): Performs no action and requires the program to handle the operation in the Windowclosing method of the registered WindowListener object.
Hide_on_close (defined in windowconstants): The form is automatically hidden after any registered WindowListener object is called.
Dispose_on_close (defined in windowconstants): Automatically hides and releases the form after calling any registered WindowListener object.
Exit_on_close (defined in JFrame): Exits the application using the System exit method. Used only in the application.
By default, this value is set to Hide_on_close. Changing the value of this property causes the property change event to be fired and its property name is "Defaultcloseoperation".
Add:
Just add or modify the line below.
This.setdefaultcloseoperation (Jframe.do_nothing_on_close);
Java jframe Close window