jTextArea as IO console

前端 未结 2 1300
無奈伤痛
無奈伤痛 2020-11-30 16:05

How do I send the input of the user, user input only, to the outputstream?

I\'m currently using a keylistner,

jTextArea console = new jTextArea;
con         


        
2条回答
  •  悲哀的现实
    2020-11-30 16:47

    I would suggest that when any other key is released, if starflag = true you store the start, and set startflag to false as you said, and when "enter" is actually pressed, you can do startflag = true; and store the end as the getCaretPosition().

    My understanding of your intentions beyond that is slightly vague, but here are my ideas: If you have an entire "conversation" of sorts going on in your console, and you want ALL of the user entries, then you could simply use an ArrayList userinputs, and instead of start = console.getCaretPosition(), you could do userinputs.add(new Point(console.getCaretPosition(),-1)); and then when the user ends his input(or before STDOUT sends input), do userinputs.get(userinputs.size()-1).y = console.getCaretPosition()(Or you could store a temporary point, and only add the point upon it's completion - when it get an end as well.)

    If you are sending the user input to the output whenever the enter key is pressed, then I don't see what more is needed than the simple code above, that essentially works like this:

    public void keyReleased(KeyEvent e){
    if(e.getKeyCode() == KeyEvent.VK_ENTER){
        startflag = true;
        //Just get the text from the start(stored below), and up until where the caret is now, as the users output.
    }else{
        if(startflag){
             start = console.getCaretPosition()
             startflag = false;
        }
    }
    }
    

提交回复
热议问题