Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8811309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:19:53+00:00 2026-06-14T03:19:53+00:00

I have made a basic hangman game on Netbeans, and the way it works

  • 0

I have made a basic hangman game on Netbeans, and the way it works is a user clicks one of the 26 buttons (one for each letter of the alphabet) and that letter is tested in the word. The main problem I’m having is getting the letter to be tested.

    //array holding all buttons        
    JButton[] alphabet = {AButton, BButton, CButton, DButton, EButton, FButton, GButton, HButton, IButton, JButton, KButton, LButton, MButton, NButton, OButton, PButton, QButton, RButton, SButton, TButton, UButton, VButton, WButton, XButton, YButton, ZButton};

    //getting the current letter(need something in place of the 0)
    String currentLetter = (alphabet[0].getLabel());             

    //replace underscores with letters as they are guessed
                for (int i = 0; i < 1; i++) {
                    secretWord = secretWord + currentLetter.charAt(0);
                    foundWord = words[randValue].replaceAll("[^" + secretWord + "]", "_ ");
    }

I read somewhere that using a button array is what I had to do, so I put it in but it didn’t work. Also, I read that an ActionListener was needed for the program to read what letter it is. Previous tries I have used were:

currentLetter = AButton.getText();

currentLetter = AButton.getLabel();

but sadly to no avail. So keep in mind that I am using Netbeans and I’m new-ish to Java, so no big elaborate hard stuff, keep it simple. Thanks:)

EDIT:

        private void AButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
        DoButton.doClick();
}                                       

        private void DoButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
            JButton button = (JButton) evt.getSource();
            String currentLetter = button.getText(); 

        //replace underscores with letters as they are guessed
                        for (int i = 0; i < 1; i++) {
                            secretWord = secretWord + currentLetter.charAt(0);
                            foundWord = words[0].replaceAll("[^" + secretWord + "]", "_ ");
               }
    }

Full Code:

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.UIManager;
    import javax.swing.plaf.FontUIResource;

    public class MainFrame extends javax.swing.JFrame {

        public MainFrame() {
            initComponents();
        }

        static String secretWord = "";
        StringBuilder mainWord = new StringBuilder();
        String[] words =         {"technology", "computer", "camera", "graphic", "digital", "media", "technician",
            "photography", "troubleshoot", "pixels", "application", "download"};
        Random r = new Random();
        int randValue = r.nextInt(11)+1;
        String guessWord = words[0];//words[randValue];
        int errors = 0;
        public static int wins = 0, losses = 0;
        String foundWord = null;
        String currentLetter;
        private void RestartButtonActionPerformed(java.awt.event.ActionEvent evt)         {                                              
            //restart game
                DirectionsFrame DFrame = new DirectionsFrame();
                DFrame.setVisible(true);
                setVisible(false);
                MainFrame MFrame = new MainFrame();
                MFrame.dispose();
                secretWord = "";
                foundWord = null;
                DirectionsFrame.WinsLabel.setText(null);
                DirectionsFrame.LossesLabel.setText(null);
        }                                             

        private void GetButtonActionPerformed(java.awt.event.ActionEvent evt)         {                                          
            for (int i = 0; i < guessWord.length(); i++) {
                mainWord.append("_ ");
            }
            String SetMain = mainWord.toString();
            mainWord.append(secretWord);
            WordLabel.setText(SetMain);
            GetButton.setEnabled(false);
            AButton.setEnabled(true);BButton.setEnabled(true);CButton.setEnabled        (true);DButton.setEnabled(true);EButton.setEnabled(true);
            FButton.setEnabled(true);GButton.setEnabled(true);HButton.setEnabled        (true);IButton.setEnabled(true);JButton.setEnabled(true);
    KButton.setEnabled(true);LButton.setEnabled(true);MButton.setEnabled(true);NButton.setEnabled(true);OButton.setEnabled(true);
    PButton.setEnabled(true);QButton.setEnabled(true);RButton.setEnabled(true);SButton.setEnabled(true);TButton.setEnabled(true);
    UButton.setEnabled(true);VButton.setEnabled(true);WButton.setEnabled(true);XButton.setEnabled(true);YButton.setEnabled(true);
    ZButton.setEnabled(true);
}                                         

private void ExitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // Exit the program
    System.exit(0);
}                                          

private void AButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void BButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void CButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void DButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void EButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void FButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void GButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void HButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void IButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void JButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void KButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void LButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void MButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void NButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void OButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void PButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void QButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void RButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void SButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void TButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void UButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void VButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void WButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void XButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void YButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void ZButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    DoButton.doClick();
}                                       

private void DoButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JButton button = (JButton) evt.getSource();
    String currentLetter = button.getText(); 

//replace underscores with letters as they are guessed
            do {
                for (int i = 0; i < 1; i++) {
                    secretWord = secretWord + currentLetter.charAt(0);
                    foundWord = words[0].replaceAll("[^" + secretWord + "]", "_ ");
                //if letter isn't in word
                    if (guessWord.indexOf(currentLetter) == -1) {
                        JOptionPane.showMessageDialog(null, "Sorry, that wasn't in the word.");
                        errors++;
                        if (errors == 1) {
                            Hangman0.setVisible(false);
                        }
                        if (errors == 2) {
                            Hangman1.setVisible(false);
                        }
                        if (errors == 3) {
                            Hangman2.setVisible(false);
                        }
                        if (errors == 4) {
                            Hangman3.setVisible(false);
                        }
                        if (errors == 5) {
                            Hangman4.setVisible(false);
                        }
                        if (errors == 6) {
                            Hangman5.setVisible(false);
                        }
                        if (errors == 7) {
                            Hangman6.setVisible(false);
                        }
                        if (errors == 8) {
                            Hangman7.setVisible(false);
                        }
                        if (errors == 9) {
                            Hangman8.setVisible(false);
                        }
                        if (errors == 10) {
                            Hangman9.setVisible(false);
                            JOptionPane.showMessageDialog(null, "You lost! The word was: " + guessWord);
                            losses++;
                            DirectionsFrame DFrame = new DirectionsFrame();
                            DFrame.setVisible(true);
                            setVisible(false);
                            MainFrame MFrame = new MainFrame();
                            MFrame.dispose();
                            secretWord = "";
                            foundWord = null;
                            String strLosses = Integer.toString(losses);
                            String strWin = Integer.toString(wins);
                            DirectionsFrame.WinsLabel.setText(strWin);
                            DirectionsFrame.LossesLabel.setText(strLosses);
                        }
            }
        }
                WordLabel.setText(foundWord.toUpperCase());
            } while (foundWord == null);
            if (foundWord.equalsIgnoreCase(guessWord)) {
                JOptionPane.showMessageDialog(null, "Yay!");
                wins++;
                DirectionsFrame DFrame = new DirectionsFrame();
                DFrame.setVisible(true);
                setVisible(false);
                MainFrame MFrame = new MainFrame();
                MFrame.dispose();
                secretWord = "";
                foundWord = null;
                String strWin = Integer.toString(wins);
                String strLosses = Integer.toString(losses);
                DirectionsFrame.WinsLabel.setText(strWin);
                DirectionsFrame.LossesLabel.setText(strLosses);
            }
}                                        
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:19:54+00:00Added an answer on June 14, 2026 at 3:19 am

    Create an action listener:

    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            Object src = evt.getSource();
            if (evt instanceof JButton) {
                currentLetter = ((JButton) src).getText();
                // do something with letter
            } // else something seriously wrong
        }
    };
    

    Then add this action listener to each of the buttons in alphabet:

    for (JButton button : alphabet) {
        button.addActionListener(al);
    }
    

    EDIT I don’t see the implementation of initComponents() in your updated code. Regardless, adding an action listener to each button is something that needs to be done only once. here’s how I would start implementing initComponents():

    private static String[] letters = { "A", "B", "C", "D", "E", "F",
        "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
        "S", "T", "U", "V", "W", "X", "Y", "Z" };
    private JButton[] letterButtons = new JButton[letters.length];
    
    private void initComponents() {
        ActionListener al = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                Object src = evt.getSource();
                if (evt instanceof JButton) {
                    currentLetter = ((JButton) src).getText();
                    // do something with letter
                } // else something seriously wrong
            }
        };
        for (int i = 0; i < letters.length; ++i) {
            letterButtons[i] = new JButton(letters[i]);
            letterButtons[i].addActionListener(al);
        }
        . . .
        // add all the buttons to the user interface
        . . .
    }  
    

    Please simplify your code by using more arrays! I certainly would not have a separate variable for each letter button or 26 separate methods to deal separately with each letter click.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All right, so I have made a basic game, in which I made a
I have made one custom user control (search text box), which basically consists of
I have made a basic ascx control which is just a panel with a
I have made a basic unordered list. <ul> <li><a>abc</a></li> <li><a>def</a></li> <li><a>ghi</a></li> <li><a>jkl</a></li> </ul> I
I am a very new programmer, I have made a couple basic applications, however
I have made a sample for posting on Facebook using the basic Facebook library
My permalink structure is set so I have url.com/page I made a basic PHP
i have an editor that is basic microsoft actions: createlink bold etc. i made
I have made a new windows service which works fine using barebone code (just
I have made a REST Web service, that works with Visual Studios WCF Test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.