简体   繁体   中英

Java JTextField as InputStream. 'ENTER' not functioning as enter

My program connects to UNIX/PUTTY in order to execute commands through a custom GUI. I have a JTextArea as my OutputStream and a JTextField as my InputStream.

The Output is functioning as expected, however Input stream is not. In order for the command to be executed within UNIX it requires the ENTER key to be pressed, however the enter key is not functioning as expected.

Here is my code:

public class JTextFieldInputStream extends InputStream {
byte[] contents;
int pointer = 0;
String toBytes = "";
public JTextFieldInputStream(final JTextField text) {

    text.addKeyListener(new KeyAdapter() {
        @Override
        public void keyReleased(KeyEvent e) {
            if(e.getKeyChar()=='\n'){
                toBytes = text.getText()+'\n';
                contents = toBytes.getBytes();
                pointer = 0;
                text.setText("");
                toBytes = "";
            }
            super.keyReleased(e);
        }
    });
}

@Override
public int read() throws IOException {
    if(pointer >= contents.length) return -1;
    return this.contents[pointer++];
}

I am trying to pass the '\\n' char as the ENTER, but doesn't seem to be working. It gets into the if statement fine and will reset the text field to "". But '\\n' toBytes () doesn't seem to be interpreted by the input stream as an enter press. Any help would be greatly appreciated. Thankyou :)

问题在于JTextField,因为它是单行,仅在您需要使用JTextArea的情况下。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM