简体   繁体   English

JLabel更新问题

[英]JLabel updating problems

I am creating a hangman game and I was having trouble getting the jlabel that contained each character of the word to update after the right letter button has been clicked. 我正在创建一个子手游戏,但是在单击右字母按钮后,无法获取包含单词的每个字符的jlabel进行更新。 I have been having trouble with this as I am relatively new to working with Java Guis. 我在使用Java Guis时还比较陌生,因此遇到了麻烦。 Below is the action listener for the letter buttons. 以下是字母按钮的动作侦听器。

private class LetterHandler implements ActionListener{

    private char letterVal;

    public LetterHandler(char lv){      
        letterVal = lv;
    }

    //checks if clicked button has the correct value as word char
    public void actionPerformed(ActionEvent e){
        for(int x = 1; x <= selectedWord.wordLength; x++){
            if(selectedWord.wordValue.charAt(x - 1) == letterVal){
//                  JLabel letterValLabel = new  JLabel(String.valueOf(letterVal));
                wordSpacesArray.get(x-1).setName(String.valueOf(letterVal));
                wordSpacesArray.get(x-1).revalidate();
                continue;
            }
            else{
                continue;
            }
        }
        checkWin();
        triesLeft--;
        triesLeftLabel.revalidate();
    }

    //finds if all jlabels are complete or not
    public void checkWin(){
        for(int x = 1; x <= wordSpacesArray.size(); x++){
            String charVal;
            charVal = String.valueOf(wordSpacesArray.get(x-1));
            if(charVal != "?"){
                System.out.println("youWon");
                System.exit(0);
            }
            else{
                break;
            }
            charVal = null;
        }
    }   
}

Any help is appreciated. 任何帮助表示赞赏。 Let me know if you need for of the programs code Thanks :) 让我知道您是否需要程序代码谢谢:)

There are some issues with the code. 代码存在一些问题。 However, I'll first try to focus on your current problem: 但是,我将首先尝试关注您当前的问题:

I assume that wordSpacesArray is a list that contains the JLabel s of individual letters of the word. 我假设wordSpacesArray是一个包含单词的各个字母的JLabel的列表。

When this ActionListener will be notified, you try to update the labels in wordSpacesArray with the letter that corresponds to this button. 当通知此ActionListener时,您尝试使用与该按钮相对应的字母来更新wordSpacesArray的标签。 However, in order to update the text that is shown on a JLabel, you have to call JLabel#setText(String) and not JLabel#setName(String) . 但是,为了更新JLabel上显示的文本,您必须调用JLabel#setText(String)不是 JLabel#setName(String) So the line should be 所以这条线应该是

wordSpacesArray.get(x-1).setText(String.valueOf(letterVal));
                      //      ^  Use setText here!

Now, concerning the other issues: 现在,关于其他问题:

  • As pointed out in the comments, you should use 0-based indexing 正如评论中指出的那样,您应该使用基于0的索引
  • The calls to revalidate are not necessary 不需要revalidate电话
  • The use of continue in its current for is not necessary 不需要在当前使用中使用continue
  • You should not compare strings with == , but with equals 应该比较字符串== ,但与equals

     // if(charVal != "?") { ... } // Don't do this! if(!charVal.equals("?")){ ... } // Use this instead 
  • But the charVal in this case will be wrong anyhow: It will be the string representation of the label, and not its contents. 但是在这种情况下, charVal无论如何都将是错误的:它将是标签的字符串表示形式 ,而不是其内容。 So you should instead obtain the text from the label like this: 因此,您应该这样从标签中获取文本:

     // String charVal = String.valueOf(wordSpacesArray.get(x-1)); // NO! String charVal = wordSpacesArray.get(x-1).getText(); // Yes! 
  • The triesLeftLabel will not be updated as long as you don't call setText on it 只要您不对triesLeftLabel调用setText ,就不会对其进行更新。

  • I think the logic of the checkWin method is flawed. 我认为checkWin方法的逻辑有缺陷。 You print "You won" when you find the first letter that is not a question mark. 当您找到不是问号的第一个字母时,您将打印“您赢了”。 I think it should print "You won" when no letter is a question mark. 我认为当没有字母是问号时,它应该打印“ You won won”。
  • You should not call System.exit(0) . 您不应调用System.exit(0) That's a bad practice. 那是一个坏习惯。 Let your application end normally. 让您的应用程序正常结束。 (In this case, maybe by just disposing the main frame, although that would also be questionable for a game...) (在这种情况下,也许只是布置主机架,尽管这对于游戏来说也是个问题……)

So in summary, the class could probably look like this: 因此,总而言之,该类可能看起来像这样:

private class LetterHandler implements ActionListener
{
    private char letterVal;

    public LetterHandler(char lv)
    {
        letterVal = lv;
    }

    // checks if clicked button has the correct value as word char
    @Override
    public void actionPerformed(ActionEvent e)
    {
        for (int x = 0; x < selectedWord.wordLength; x++)
        {
            if (selectedWord.wordValue.charAt(x) == letterVal)
            {
                wordSpacesArray.get(x).setText(String.valueOf(letterVal));
            }
        }
        checkWin();
        triesLeft--;
        triesLeftLabel.setText(String.valueOf(triesLeft));
    }

    // finds if all jlabels are complete or not
    public void checkWin()
    {
        for (int x = 0; x < wordSpacesArray.size(); x++)
        {
            String charVal = wordSpacesArray.get(x).getText();
            if (charVal.equals("?"))
            {
                // There is still an incomplete label
                return; 
            }
        }

        // When it reaches this line, no incomplete label was found
        System.out.println("You won");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM