If you want to close only one frame, you should do something like this: setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
If you want to close all frames whenever one frame is closed, you can do the following:
You can use the window listener and call System.exit(0); when the JFrame closes, or try setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); on every JFrame . Thus, your program will close all frames and end.
If you need to complete some tasks before shutting down the application, you should probably use a window listener.
source share