title: Opening double amount of new frames on every click
body: <p>Have a little problem with some code i have writing to try out something. I have made a frame with a single button in it. When i click on this button, a new frame opens, which it should. I close down the new frame, and then click on the button again, to try see if it still works. The problem starts here, corse insted of opening a single new frame, it opens two new frames. Third time i click it opens 4 frames and so on. I have tried quite a few things, but sadly cant seem to find the reason why it is opening more frames. Please help.</p>
  
  <pre><code>package budget;
  
  import java.awt.event.*;
  import javax.swing.*;
  
  public class GUI extends JFrame {
  
      String labelPrefix;
      JButton button;
      JButton button2;
      JLabel label;
  
      public static void main(String[] args) {
          JFrame f = new GUI();
          f.setExtendedState(f.MAXIMIZED_BOTH);
          f.setVisible(true);
      }
  
      public GUI() {
          JPanel p = new JPanel();
          p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
          p.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
  
          button = new JButton("Click Me");
          label = new JLabel(labelPrefix);
          p.add(button);
          this.setTitle("Try");
          getContentPane().add(p);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          pack();
          button.addActionListener(new MyActionListener());
      }
  
      class MyActionListener implements ActionListener {
  
          public void actionPerformed(ActionEvent e) {
              button.addActionListener(this);
              labelPrefix = "Try";
              JFrame f2 = new GUI(label, labelPrefix);
              f2.setExtendedState(f2.MAXIMIZED_BOTH);
              f2.setVisible(true);
  
          }
      }
  
      public GUI(JLabel label, String labelPrefix) {
          JPanel p2 = new JPanel();
          button2 = new JButton("Close");
          p2.add(label);
          p2.add(button2);
          this.setTitle("Try");
          getContentPane().add(p2);
          this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          pack();
          button2.addActionListener(new MyActionListener2());
      }
  
      class MyActionListener2 implements ActionListener {
  
          public void actionPerformed(ActionEvent e) {
              button2.addActionListener(this);
              dispose();
          }
      }
  }
  </code></pre>