title: Java hangman. How to create multiple buttons and actionlisteners?
body: <p>I am trying to create a hangman game with a button for each letter of the alphabet. I have done this the hard way, using absolute positioning and multiple actionlisteners. Is there any way I can do both with a for loop?</p>
  
  <p>Also, how can I implement my hangman using a polymorphic array? The way I have it now, I will use an if statement for each of the limbs. I'd wrather create the man on his own panel, then set the visibility for each limg to true as the use fails at guessing.</p>
  
  <p>Any help is appreciated.</p>
  
  <pre><code>public class HangmanGui extends JFrame{
      private JLabel headerLabel;
      private JPanel man;
      private Graphics gobj;
  
      private JButton aButton ;
      private JButton bButton ;
      private JButton cButton ;
      private  JButton dButton ;
      private  JButton eButton ;
      private  JButton fButton ;
      private  JButton gButton ;
      private  JButton hButton ;
      private  JButton iButton ;
      private  JButton jButton ;
      private  JButton kButton ;
      private  JButton lButton ;
      private  JButton mButton ;
      private  JButton nButton ;
      private  JButton oButton ;
      private  JButton pButton ;
      private  JButton qButton ;
      private  JButton rButton ;
      private  JButton sButton ;
      private  JButton tButton ;
      private  JButton uButton ;
      private  JButton vButton ;
      private  JButton wButton ;
      private  JButton xButton ;
      private  JButton yButton ;
      private  JButton zButton ;
      private  JButton newWButton ;
      private JButton showWButton ;
      private  JButton quitButton ;
      private  JButton startButton ;
  
      private JLabel blankWord;
  
      private JLabel titleWord;
  
      private JFrame frame;
      private JPanel hangman;
      private FlowLayout layout;
      private Container container;
  
  
  
      /*  
      public static void main (String[] args){
          GUITest gui = new GUITest();
  
          gui.setSize(800,900);
          gui.setVisible(true);
  
      }
   */ 
       //
      //   public GUITest()
     //  {
  
  
  
     //  }
  
  
  
      public HangmanGui(){
          buildGui();
  
      }
  
  
  
  
  
      public void buildGui(){
          frame = new JFrame();
          frame.getContentPane().setBackground(Color.WHITE);
          frame.setBackground(Color.WHITE);
          frame.setBounds(100, 100, 450, 600);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().setLayout(null);
          Font lblFont= new Font("Serif", Font.BOLD, 30); 
           JLabel[] uscore = new JLabel[15];
  
          Man man = new Man();
  
          titleWord = new JLabel("A Game of Hangman.");
          titleWord.setBounds(260,10,500,150);
          titleWord.setFont(lblFont);
  
  
          add(titleWord);
      //  add(blankWord);
          //frame.add(man);
          this.add(man);
          man.setBounds(100,100,400,400);
          JPanel panel = new JPanel();
  
  
          panel.setBounds(6, 232, 400, 400);
          frame.getContentPane().add(panel);
  
  
  
          layout = new FlowLayout();
           container = getContentPane();
           setLayout(null);
  
  
           aButton = new JButton("A");
  
           add(aButton);
           aButton.setBounds(30, 520, 50, 29);
          aButton.addActionListener(
                   new ActionListener()
                   {
                   @Override
                      public void actionPerformed(ActionEvent event)
                      {
  
                      }
                   }
                   );
  
  
  
             bButton = new JButton("B");
           add(bButton);
          bButton.setBounds(80, 520, 50, 29);
          bButton.addActionListener(
                   new ActionListener()
                   {
                   @Override
                      public void actionPerformed(ActionEvent event)
                      {
  
                      }
                   }
                   );
          cButton = new JButton("C");
       add(cButton);
      cButton.setBounds(130, 520, 50, 29);
      cButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
      dButton = new JButton("D");
       add(dButton);
       dButton.setBounds(180, 520, 50, 29);
      dButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
         eButton = new JButton("E");
       add(eButton);
      eButton.setBounds(230, 520, 50, 29);
      eButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
      fButton = new JButton("F");
       add(fButton);
       fButton.setBounds(280, 520, 50, 29);
      fButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
         gButton = new JButton("G");
       add(gButton);
      gButton.setBounds(330, 520, 50, 29);
      gButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
      hButton = new JButton("H");
       add(hButton);
       hButton.setBounds(380, 520, 50, 29);
      hButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
         iButton = new JButton("I");
       add(iButton);
      iButton.setBounds(430, 520, 50, 29);
      iButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
      jButton = new JButton("J");
       add(jButton);
       jButton.setBounds(480, 520, 50, 29);
      jButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
         kButton = new JButton("K");
       add(kButton);
      kButton.setBounds(530, 520, 50, 29);
      kButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
      lButton = new JButton("L");
       add(lButton);
       lButton.setBounds(580, 520, 50, 29);
      lButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
         mButton = new JButton("M");
       add(mButton);
      mButton.setBounds(630, 520, 50, 29);
      mButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
      nButton = new JButton("N");
       add(nButton);
       nButton.setBounds(680, 520, 50, 29);
      nButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
         oButton = new JButton("O");
       add(oButton);
      oButton.setBounds(30, 550, 50, 29);
      oButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
      pButton = new JButton("P");
       add(pButton);
       pButton.setBounds(80, 550, 50, 29);
      pButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
         qButton = new JButton("Q");
       add(qButton);
      qButton.setBounds(130, 550, 50, 29);
      qButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
      rButton = new JButton("R");
       add(rButton);
       rButton.setBounds(180, 550, 50, 29);
      rButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
         sButton = new JButton("S");
       add(sButton);
      sButton.setBounds(230, 550, 50, 29);
      sButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
      tButton = new JButton("T");
       add(tButton);
       tButton.setBounds(280, 550, 50, 29);
      tButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
         uButton = new JButton("U");
       add(uButton);
      uButton.setBounds(330, 550, 50, 29);
      uButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
      vButton = new JButton("V");
       add(vButton);
       vButton.setBounds(380, 550, 50, 29);
      vButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
         wButton = new JButton("W");
       add(wButton);
       wButton.setBounds(430, 550, 50, 29);
      wButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
      xButton = new JButton("X");
       add(xButton);
       xButton.setBounds(480, 550, 50, 29);
      xButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
         yButton = new JButton("Y");
       add(yButton);
       yButton.setBounds(530, 550, 50, 29);
      yButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
      zButton = new JButton("Z");
       add(zButton);
       zButton.setBounds(580, 550, 50, 29);
      zButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
  
       startButton = new JButton("Start Game");
       add(startButton);
       startButton.setBounds(100, 700, 120, 29);
      startButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
  
       newWButton = new JButton("New Word");
       add(newWButton);
       newWButton.setBounds(250, 700, 120, 29);
      newWButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
       showWButton = new JButton("Show Word");
       add(showWButton);
       showWButton.setBounds(400, 700, 120, 29);
      showWButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
  
       quitButton = new JButton("Quit Game");
       add(quitButton);
       quitButton.setBounds(550, 700, 120, 29);
      quitButton.addActionListener(
               new ActionListener()
               {
               @Override
                  public void actionPerformed(ActionEvent event)
                  {
  
                  }
               }
               );
  
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>import java.awt.Graphics;
  import java.awt.Color;
  import javax.swing.JPanel;
  
  
  public class Man extends JPanel {
  
       protected void paintComponent(Graphics g ){
                  super.paintComponent(g);
  
  
         //gallows
             g.fillRect(10, 250, 150, 20);
             g.fillRect(40,70,10,200);
             g.fillRect(40,70,60,10);
             g.setColor(Color.yellow);
             g.fillRect(95,70,5,25);
            //head
             g.setColor(Color.black);
             g.drawOval(82,95,30,30);
  
             //body
             g.drawLine(97,125,97,150);
            //left leg
             g.drawLine(97,150,117,183);
             //right leg
            g.drawLine(97,150,77,183);
             // right arm
             g.drawLine(97,125,117,135);
             //left arm
             g.drawLine(97,125,77,135);
  
  
  
  
  }
  </code></pre>
  
  <p>}</p>