title: Make a JPanel Change After ActionListener
body: <p>I have to make a game for a school project in CS. I want to make it so that a person may input their name and all subsequent panels will have their name on it. I am using a card layout, which works for the most part. When I run the game and input the name, it works specifically on that card. However, when I go to the next card, where the String name is supposed to appear, it appears as null. My suspicion is that the program creates all of the cards before given the chance to accept the name input and is therefore failing. My teacher doesn't really know how to do it either. </p>
  
  <p>Here is a segment of the code: </p>
  
  <pre><code>    import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      public class NovelCards extends JFrame {
     private String name;
     private CardLayout cards;
     private JTextField txt = new JTextField(10);
     private JLabel label = new JLabel();
     public NovelCards() {
        super("Novel");
        masterPanel = new JPanel();
        cards = new CardLayout();
        txt.addActionListener( new ActionListener(){
            public void actionPerformed(ActionEvent e){
              name = txt.getText();
              label.setText(name);
            }
        });      
        button2 = new JButton("Next");
        button2.addActionListener(new NextListener());
        JPanel card2 = new JPanel();
        card2.add(button2);
        card2.add(txt);
        card2.add(label);
        card2.add(new JLabel(" Enter your name "));
  
        JButton button3 = new JButton("Next");
        button3.addActionListener(new NextListener());
        JPanel card3 = new JPanel();
        card3.add(new JLabel("Hello there, " + name + "!"));
        card3.add(button3);
  
        masterPanel.setLayout(cards); 
        masterPanel.add("2",card2);
        masterPanel.add("3",card3);
        this.setContentPane(masterPanel);
        cards.show(masterPanel, "1");
   }
  
   public class NextListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
       cards.next(masterPanel);
   }
  </code></pre>
  
  <p>Here are also some photos of the program when running. </p>
  
  <p>Here is the name enter screen after picking a name and hitting enter: </p>
  
  <p><img src="https://i.stack.imgur.com/AC446.png" alt="http://i.stack.imgur.com/AC446.png"></p>
  
  <p>And then here is the subsequent card:</p>
  
  <p><img src="https://i.stack.imgur.com/Z1Jin.png" alt="http://i.stack.imgur.com/Z1Jin.png"></p>
  
  <p>Thanks in advance for the help! (I would have directly posted the images but I don't have enough reputation)</p>