title: How to stop Swing from opening a new frame?
body: <p>I'm writing a Java text adventure and using Java Swing as a way to display a simple user GUI as the game play screen. I want to have three different windows, one as a start screen, one for a character creation screen where the user inputs a name and some attributes, and one where the game actually takes place. </p>
  
  <p>I want to write each screen in a separate class to increase the organization and readability of my code. However whenever I click on the <code>JButton</code> to go to the next screen a completely new <code>JFrame</code> opens with the content. How can I write my GUI so that all of the screen changes take place in just one frame? Is Java supposed to function like this? </p>
  
  <p>I've looked through at least a couple dozen Java Swing forum questions but still cant find out what I'm doing wrong with my Java swing implementation. </p>
  
  <pre><code>package guiPackage;
  
  import javax.swing.*;
  import thingsPackage.Player;
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  public class StartGame extends JFrame {
  
  JFrame Game;
  JPanel buttonMenu, screen;
  JButton newGame, settings, exit, loadGame;
  
  
      public StartGame() {
  
      Game = new JFrame();
      Game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
      Game.setVisible(true);
      Game.setSize(750, 750);
      Game.setLocationRelativeTo(null);
  
      screen = new JPanel();
      screen.setVisible(true);
  
      TitleScreenHandler ts = new TitleScreenHandler();
  
      buttonMenu = new JPanel();
  
      newGame = new JButton("New Game");
      newGame.addActionListener(ts);
      loadGame = new JButton("LoadGame");
      loadGame.addActionListener(ts);
  
  
      settings = new JButton("Settings");
      settings.addActionListener(ts);
  
      exit = new JButton("Exit");
      exit.addActionListener(ts);
  
      Game.add(screen);
      screen.add(buttonMenu, BorderLayout.SOUTH);
      buttonMenu.add(newGame);
      buttonMenu.add(loadGame);
      buttonMenu.add(settings);
      buttonMenu.add(exit);
  
  }
  
  
  public class TitleScreenHandler implements ActionListener{
      public void actionPerformed(ActionEvent titleScreenEvent) {
  
          if (titleScreenEvent.getSource() == newGame) {
              buttonMenu.setVisible(false);
              CharacterCreator newCharacter = new CharacterCreator();
  
          }
          else if (titleScreenEvent.getSource() == loadGame) {
              buttonMenu.setVisible(false);
          }
          else if (titleScreenEvent.getSource() == settings) {
              buttonMenu.setVisible(false);
  
          }
          else if (titleScreenEvent.getSource() == exit){
              System.out.println("goodbye");
              System.exit(0);
          }
      }
  }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>package guiPackage;
  
  import javax.swing.*;
  import java.awt.*;
  import java.util.Scanner;
  
  public class CharacterCreator extends StartGame {
  
  JTextField newName;
  JPanel creatorScreen;
  Scanner scan = new Scanner(System.in);
  
  public CharacterCreator() {
  
      creatorScreen = new JPanel();
      //creatorScreen.setVisible(true);
  
      newName = new JTextField("Create a charactor");
      //newName.setLocation(null);
  
      screen.add(creatorScreen);
      creatorScreen.add(newName);
  }
  public String setCharacterName(){
      String characterName = "";
      return characterName;
  }
  }
  </code></pre>