Hangman:如果正确的按钮点击应用程序挂起

时间:2012-09-02 21:04:05

标签: java

在我的刽子手应用程序中,我为用户创建了一组按钮,然后从文件中随机选择了在刽子手应用程序中猜到的单词...这个单词是使用JLabel打印的...但是很快单击一个按钮,应用程序冻结。任何人都可以告诉我这种情况正在发生......


我如何创建按钮

for(char i = 'A'; i <= 'Z'; i++){
    String buttonText = new Character(i).toString();
    JButton button = getButton(buttonText);
    panel1.add(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;
}

1 个答案:

答案 0 :(得分:3)

如果actionPerformed小于6,则error会有无限循环。

while(error < 6) {
}