Hangman游戏从JTextField获取用户输入

时间:2015-11-28 17:32:07

标签: java swing

我试着写一个简单的Hangman游戏,到目前为止我已经设法得到单词长度和随机选择的单词。但是,我无法找到实现用户输入的方法。这个想法是用户在JTextField中输入一个字母,如果正确猜出了字母,则替换XXX。如果有人可以帮我解决这个问题,我将不胜感激,请查看我到目前为止的代码,并请注意这是在NetBeans中完成的。

static String [] words = { "dog", "java" };
int[] wordLength = new int[2];
String line = "",letter;    
int level = (int) (Math.random() * 2);    
int a = 0,b = 0, wrong=0;

private void btGuessActionPerformed(java.awt.event.ActionEvent evt) {                                        
    for (a = 0; a < 2; a++) {
        wordLength[a] = words[a].length();
    } 
    while (b < wordLength[level]) {
        line += " X";
        b++;
    }

    Letter0.setText(line);
    letter = uInput.getText();
    if (!words[level].contains(letter)) {
        wrong++;
        if (wrong == 1) {
            pbShow.setIcon(new javax.swing.ImageIcon("images/H0.png"));
        }
        if (wrong == 2) {
            pbShow.setIcon(new javax.swing.ImageIcon("images/H1.png"));
        }
        if (wrong == 3) {
            pbShow.setIcon(new javax.swing.ImageIcon("images/H2.png"));
        }
        if (wrong == 4) {
            pbShow.setIcon(new javax.swing.ImageIcon("images/H3.png"));
        }
        if (wrong == 5) {
            pbShow.setIcon(new javax.swing.ImageIcon("images/H4.png"));
        }
        if (wrong == 6) {
            pbShow.setIcon(new javax.swing.ImageIcon("images/H5.png"));
        }
        if (wrong == 7) {
            pbShow.setIcon(new javax.swing.ImageIcon("images/Hlost.png"));
        }              
    }
}

0 个答案:

没有答案