前兩天寫聊天室程式的時候, 想模仿MSN,
只要使用者按enter key,便可把輸入區的文字,寫入outputStream,並清空輸入區的文字。
文字輸入區我是用JScrolPane包JTextArea,
並在JTextArea中的enter key事件驅動時,
呼叫JTextArea的setText("")來清空輸入區的文字。
結果發生一件詭異的事,
那就是雖然輸入區的文字是如期清空了,
但游標卻總是停在第二行的開頭,而不是停在首行的開頭。
我試了在keyPressed()裡去改變最後JTextArea的caret位置,如setCaretPosition()等...
但就是無法把caret移到首行的開頭。
最後終於在google上找到解法。
來源網址http://en.allexperts.com/q/Java-1046/JTextArea-caret-postion.htm
Question
Sir, I was making one application which has 2 JTextArea's and 1 button.
When i enter some text in textArea-2 and press 'Enter Key', it should transefer the text to textArea-1 and must empty the textArea-2 and now the focus should be present on the button(that [...]


