在while循环期间获取TextField输入而不是JOptionPane java

时间:2013-08-29 11:04:46

标签: java while-loop textfield

当我的程序进入while循环时,我很难从Textfield获取用户输入。以前我的程序使用了JOptionPane,但现在我依靠按钮点击查询我的引擎类信息我得到了很多错误

这是我的引擎

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");

                    }
     }

}



}

您可以提供给我的任何信息都会非常有用,如果这太不明确,我会很乐意发布您需要的更多信息:)

1 个答案:

答案 0 :(得分:0)

此代码中JOptionpane的目的是什么?在给定的代码中,JOptionPane和JTextField之间没有关系。无论你是否放置了JoptionPane,你都可以从textfield获得输入。  eg-String strTemp = textfield.getText(); //这将为您提供文本字段值。

我认为您将此getText()值直接传递给接受字符的方法enginer()。您是否收到无效的参数错误?