0

In my hangman application I've created a set of buttons for the user, and then the word being guessed in hangman app is randomly selected from a file... The word is printed using an JLabel... But as soon as a button is clicked the application freezes. Can anyone please tell my this is happening...


How I created the buttons

for(char i = 'A'; i <= 'Z'; i++){
    String buttonText = new Character(i).toString();
    JButton button = getButton(buttonText);
    panel1.add(button);
}

button ActionListener

public JButton getButton(final String text){
    final JButton button = new JButton(text);
    button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(original.toUpperCase().indexOf(button.getText())!=-1){
                guessString = text;
                guessLetter = guessString.charAt(0);
                StringBuilder builder = new StringBuilder(secret);
                while(error < 6){
                }
                for (int i = 0; i < original.length(); i++){
                    if (original.charAt(i) == guessLetter){
                        builder.setCharAt(i, guessLetter);
                    }
                }
                secret = builder.toString();
            }
            else{
                JOptionPane.showMessageDialog(null, "There is no " + text );
                error++;
                if(error >= 0) imageName = "hangman1.jpg";
                if(error >= 1) imageName = "hangman2.jpg";
                if(error >= 2) imageName = "hangman3.jpg";
                if(error >= 3) imageName = "hangman4.jpg";
                if(error >= 4) imageName = "hangman5.jpg";
                if(error >= 5) imageName = "hangman6.jpg";
                if(error >= 7) imageName = "hangman7.jpg"; 
            }
        }
    });
    return button;
}
Baz
  • 36,440
  • 11
  • 68
  • 94
Night Programmer
  • 237
  • 1
  • 5
  • 14

1 Answers1

3

You have an infinite loop in actionPerformed if error is less than 6.

while(error < 6) {
}
João Silva
  • 89,303
  • 29
  • 152
  • 158