简体   繁体   English

在while循环中获取TextField输入,而不是JOptionPane Java

[英]Get TextField input during while loop instead of JOptionPane java

I'm struggling to get the user input from a Textfield when my program enters a while loop. 当我的程序进入while循环时,我很难从文本字段获取用户输入。 Previously my program used a JOptionPane but now that i am relying on a button click to sen information to my engine class i get many errors 以前我的程序使用了JOptionPane,但是现在我依靠单击按钮将信息传递给我的引擎类,因此出现很多错误

Here is my engine 这是我的引擎

public class engine  {
public final Object buttonClickedLock = new Object(); // possible clean up
guess gs = new guess();
public  boolean close = true;
char [] charda = new char[20];
char [] charwo = new char[20];
Highscore words = new Highscore();
main mn = new main();
int guesses =7;
char guess;


public engine() {

}

public void enginer(char guess) { //throws for wait method


    int count = 0;


    String word = words.getWord();


    for(int i = 0; i<word.length(); i++)
    {
        //instantiates two arrays one of dahses and one with the word
        charwo[count] = word.charAt(i);
        charda[count]= '_';
        count++;
    }

    for(int l=0; l<count; l++)
        {
            System.out.print(" "+charda[l]);

        }

    while(guesses !=0 && !Arrays.equals(charwo, charda))
    {
        guess = guess; //This is previously where my JoptionPane went 



        if(word.toUpperCase().contains(String.valueOf(guess).toUpperCase()))
        {


            for(int k = 0; k<word.length(); k++)
            {
                if(String.valueOf(guess).toUpperCase().equals(String.valueOf(charwo[k]).toUpperCase()))
                {
                            charda[k]=charwo[k];

                                    for(int l=0; l<count; l++)
                            {   //prints dashes here to avoid a letter being chopped off by the program stopping in the middle
                                System.out.print(" "+charda[l]);

                            }
                }

              }

        }

        else
        {

                guesses = guesses-1;

                System.out.println("guesses left "+guesses);
                //Re-displays dashes 
                for(int l=0; l<count; l++)
                {
                System.out.print(" "+charda[l]);

                }

        }

           if(Arrays.equals(charwo, charda))
           {
               System.out.println("");
           System.out.println("You are Winner");

                    }
     }

}



}

Any information you can provide me with would be really helpful and if this is too unspecific i will happily post as much more info as you need :) 您可以提供给我的任何信息都将非常有帮助,如果这个信息不太明确,我会很乐意根据您的需要发布更多信息:)

What was the purpose of JOptionpane in this code? 此代码中JOptionpane的目的是什么? There no relation between JOptionPane and JTextField in the given code. 在给定的代码中,JOptionPane和JTextField之间没有关系。 Whether you put a JoptionPane or not the you can always get input from textfield. 不管您是否放置JoptionPane,都可以始终从文本字段获取输入。 eg- String strTemp = textfield.getText();// this will give you the text field value. 例如:String strTemp = textfield.getText(); //这将为您提供文本字段值。

I think you are passing this getText() value directly to the method enginer() which accepts character. 我认为您是将此getText()值直接传递给接受字符的方法enginer()。 Are you getting invalid argument error? 您是否收到无效的参数错误?

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

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