title: Minesweeper Object GUI
body: <p>I am trying to do a simple Minesweeper game using <code>JFrame</code>, however I am having troubles with the creation of objects. I am creating 96 buttons, some of which get the property of being wrong ("F") and right ("R"):</p>
  
  <pre><code>public class GUIBase extends JFrame {
  
  private JButton button;
  private JButton fButton;
  
  
  public GUIBase() {
      super("Minesweeper");
      setLayout(new FlowLayout());
  
      //Fields
      int position;
      for (int i = 0; i &lt; 96; i++) {
          position = (int) (Math.random() * 100);
          if (position &lt; 80) {
              button = new JButton("R");
              button.setToolTipText("Is this the correct one?");
              add(button);
          } else {
              fButton = new JButton("F");
              fButton.setToolTipText("Is this the correct one?");
              add(fButton);
          }           
      }
  </code></pre>
  
  <p>I then use <code>ActionListener</code> in order to check whether or not the button is correct. If the button is correct, it will get <code>.setEnabled(false)</code>, otherwise the game ends:</p>
  
  <pre><code>    //Action
      Action action = new Action();
      button.addActionListener(action);
      fButton.addActionListener(action);
  
  }
  
  private class Action implements ActionListener {
  
      public void actionPerformed(ActionEvent event) {
          System.out.println("Somethin");
  
          if (event.getSource() == button) {
              button.setEnabled(false);
          } else if (event.getSource() == fButton) {
              JOptionPane.showMessageDialog(null, "You lost!");
              System.exit(0);
          } else {
              JOptionPane.showMessageDialog(null, "An error ocurred");
              System.exit(0);
          }           
      }
  } 
  </code></pre>
  
  <p>Everything in the game turns out as planned, however only the last correct button ("R") and last wrong one ("F") are connected to the <code>ActionListener</code>. The rest of the buttons do not do anything when pressed. </p>
  
  <p>How can I fix this?</p>