System.exit(0) will close the whole application. You need to maintain areference to JFrame B and call the dispose() method on it when you wantto close it.