这个实施怎么样
import java.io.IOException;import java.io.InputStream;import javax.swing.JTextField;public class JTextFieldInputStream extends InputStream { byte[] contents; int pointer = 0; public JTextFieldInputStream(final JTextField text) { text.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent e) { if(e.getKeyChar()=='n'){ contents = text.getText().getBytes(); pointer = 0; text.setText(""); } super.keyReleased(e); } }); } @Override public int read() throws IOException { if(pointer >= contents.length) return -1; return this.contents[pointer++]; }}
要使用此输入流,请执行以下 *** 作
InputStream in = new JTextFieldInputStream( someTextField ); char c; while( (c = in.read()) != -1){ //do whatever with c }
它只有在我按Enter键时才读吗?
它在您调用时读取,
in.read()如果返回值
-1表示流结束
(并且我将能够进行修改,以使Enter键清空JTextField吗?)
您需要添加一个动作侦听器,并且此功能与输入流的工作无关
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)