Closing a JFrame
application can be done by setting the default close operation of the frame. There are some defined constants for the default operation. These constants defined in the javax.swing.WindowConstants
interface include EXIT_ON_CLOSE
, HIDE_ON_CLOSE
, DO_NOTHING_ON_CLOSE
and DISPOSE_ON_CLOSE
.
To exit an application we can set the default close operation to EXIT_ON_CLOSE
, this option will call the System.exit()
method when user initiate a close operation on the frame.
package org.kodejava.swing;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class MainFrameClose extends JFrame {
public static void main(String[] args) {
MainFrameClose frame = new MainFrameClose();
frame.setSize(500, 500);
// Be defining the default close operation of a JFrame to
// EXIT_ON_CLOSE the application will be exited by calling
// System.exit() when user initiate a close event on the
// frame.
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024