[英]Java - Problems with Scanner and JTextFields
This is my first time using Scanner
in a JFrame
with 1 JTextFields
and 1 JTextArea
and I have a problem. 这是我第一次在具有1个
JTextFields
和1个JTextArea
的JFrame
使用Scanner
,但遇到了问题。 When I enter string in the textfield, the scanner is able to pick that up with no problem and I can print it out too. 当我在文本字段中输入字符串时,扫描仪可以毫无问题地拾取它,我也可以将其打印出来。
But when I try to enter a string in the textarea, the scanner doesn't pick it up. 但是,当我尝试在文本区域中输入字符串时,扫描仪不会将其拾取。 I have tried setting focusable to false on the textfield, but still the scanner is not picking up the string entered in the textarea.
我曾尝试在textfield上将focusable设置为false,但是扫描器仍然无法拾取在textarea中输入的字符串。
Any idea why this is happening? 知道为什么会这样吗?
EDIT My mistake. 编辑我的错误。 It is 1
JTextField
and 1 JTextArea
. 它是1
JTextField
和1 JTextArea
。
This is my Client class: 这是我的客户类:
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");
}
}
I don't think Scanner
is used in Swing (unless you are talking about a barcode scanner). 我不认为Swing中使用了
Scanner
(除非您正在谈论条形码扫描器)。 I believe you want use something like 我相信你想使用类似
String someText = textField1.getText();
Check out How to Use Text Fields . 查看如何使用文本字段 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.