Java JTextArea - Adding a newline

后端 未结 4 1326
孤街浪徒
孤街浪徒 2021-01-28 08:00

So I\'m stumped. I, for some reason, seem to remember that Java does weird things when you try to add new lines to stuff... so I think that may be the problem here.

Thi

4条回答
  •  滥情空心
    2021-01-28 08:47

    I'm not sure I understand what you are trying to do, all I know is that you are using the wrong approach. You should NOT be using a KeyListener.

    From what I can see you want the contents of the output text area to be the same as the contents of the input area. If this is your requirement then you just use:

    JTextArea input = new JTextArea(...);
    JTextArea output = new JTextArea(...);
    output.setDocument( input.getDocument() );
    

    Or if you are trying to listen for all the changes to the input text area then you should be using a DocumentListener. The Swing tutorial has a section titled "How to Write a Document Listener" for more information.

提交回复
热议问题