2013-08-29 42 views
0

當我的程序進入while循環時,我努力從文本字段獲取用戶輸入。以前我的程序中使用的JOptionPane的,但現在,我正在依靠點擊一個按鈕,以孫中山信息到我的引擎類,我得到了許多錯誤在While循環中獲取TextField輸入而不是JOptionPane java

這裏是我的引擎

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

        } 
    } 

} 



} 

的任何信息,你可以爲我提供會真的很有幫助,如果這太沒有針對性,我會很樂意發佈更多的信息,因爲你需要:)

+0

你的textField在哪裏,你在哪裏使用它?你的while循環在哪裏? –

回答

0

這段代碼中JOptionpane的用途是什麼?在給定的代碼中,JOptionPane和JTextField之間沒有關係。無論您是否使用JoptionPane,您始終可以從文本字段獲得輸入。 例如:String strTemp = textfield.getText(); //這會給你文本字段的值。

我想你是直接將此getText()值傳遞給接受字符的方法引擎()。你收到無效的參數錯誤?

+0

JOptionPAne的目的是讓用戶猜測,而現在我試圖實現一個Textfield來猜測。當我使用一個JOptionPane的過程如下:一個彈出對話框停止了程序等待猜測,猜測繼續和重複,直到用戶猜測或得到正確的單詞現在程序只需要第一個猜測(點擊文本框中的按鈕),並循環通過那巨大的次數。我正在尋找一種方法,可能會停止在同一點的程序,並等待用戶輸入 – nmu