我正在寻找有关如何将System.in
替换为InputStream
的说明,该文件直接从JTextField
中读取。将System.in设置为从JTextField读取
到目前为止,我的方法几乎是反复试验。我现在有;
JTextField input = new JTextField();
System.setIn(new InputStream() {
int ptr = 0;
@Override
public int read() throws IOException {
int c;
try {
c = input.getText().charAt(ptr);
}
catch (IndexOutOfBoundsException ioob) {
return 0;
}
ptr++;
return c;
}
});
这给出了NoSuchElementException
在尝试当输入是空的,我以为永远无法找到一个分隔符来阅读。
我错过了什么方法?
为什么不使用'KeyListener'来检测每个键入的类型? – evanwong
@evanwong你可以建议如何绑定到输入流? – Jivings