Java jframe Close window

Source: Internet
Author: User
Tags java jframe

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.