2015-04-07 145 views
0

這是我第一次使用ScannerJFrame與1 JTextFields和1 JTextArea我有問題。當我在文本框中輸入字符串時,掃描儀能夠毫無問題地選擇該字符串,並且我也可以將其打印出來。Java - 掃描儀和JTextFields的問題

但是,當我嘗試在textarea中輸入一個字符串時,掃描器不會將其拾取。我已經嘗試在文本字段上將focusable設置爲false,但掃描儀仍然沒有拾取在textarea中輸入的字符串。

任何想法爲什麼會發生這種情況?

編輯 我的錯誤。它是1 JTextField和1 JTextArea

這是我的客戶端類:

public class Client implements Runnable{ 

//Globals 
Socket sock; 
Scanner input; 
Scanner send = new Scanner(System.in); 
PrintWriter output; 

public Client (Socket X) { 
    this.sock = X; 
} 

//@Override 
public void run() { 
    try { 
     try { 
      input = new Scanner(sock.getInputStream()); 
      output = new PrintWriter(sock.getOutputStream()); 
      output.flush(); 
      CheckStream(); 
     } 
     finally { 
      sock.close(); 
     } 
    } 
    catch (Exception E) { 
     System.out.print(E); 
    } 
} 

public void Disconnect() throws IOException{ 
    output.println(ChatRoom.Username + " has disconnected!"); 
    output.flush(); 
    sock.close(); 
    JOptionPane.showMessageDialog(null, "You disconnected!"); 
    System.exit(0); 
} 

public void CheckStream() { 
    while (true) { 
     Receive(); 
    } 
} 

public void Receive() { 
    if (input.hasNext()) { 
     String message = input.nextLine(); 
     //the problem is with "message" 
     //it can read the first textfield but not the second 
     System.out.println(message); 

     if (message.contains("#?!")) { 
      String tempCurrUsers = message.substring(3); 
      tempCurrUsers = tempCurrUsers.replace("[", ""); 
      tempCurrUsers = tempCurrUsers.replace("]", ""); 

      String[] CurrentUsers = tempCurrUsers.split(", "); 

      ChatRoom.JL_CurrentUsersDisplay.setListData(CurrentUsers); 
     } 
     else { 
      System.out.println(message); 
      ChatRoom.TA_ChatDisplay.append(message + "\n"); 
     } 
    } 
} 

public void Send(String X) { 
    output.println(ChatRoom.Username + ": " + X); 
    output.flush(); 
    ChatRoom.TF_MessageBox.setText(""); 
    //ChatRoom.TA_ChatDisplay.append(ChatRoom.Username+": " + X + "\n"); 

}} 
+0

顯示您的代碼。 – Pratik

+0

爲了更好地提供幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)(最小完整可驗證示例)或[SSCCE](http://www.sscce.org/)(Short,Self包含,正確示例)。 –

+0

@AndrewThompson你在標籤中的意思是? –

回答

2

我不認爲Scanner在搖擺中使用(除非你是在談論一個條形碼掃描儀)。我相信你想使用類似

String someText = textField1.getText(); 

退房How to Use Text Fields