日期:2014-05-20  浏览次数:20660 次

求高手点拨“将我的聊天界面增加:根据用户的输入文本焦点”进行跟踪显示的“留言板”功能.
高手,您好:
    我在做一个IM工具,由于初学Java不久,我很希望能够向我的“JTextArea对象”中,添加我的“根据用户的输入”进行“留言板跟踪输出”的效果:
    既能够将用户接收到的“超出留言板的边界的"聊天消息"”进行“将已经占满用户的留言板的数据进行上顶”的功能实现.
    据说需要借助到:
    (JTextArea)recvArea.setCaretPosition(recvArea.getText().length()),
    将光标移到最后,实现滚动条的自动滚动。
    的功能.
    希望高手能够共享:
    上面的功能实现的“核心代码”,是什么....
    谢谢高手的相助!!

------解决方案--------------------
这是JTextComponent的函数吧。简单来说就是设置插入符的位置

public void setCaretPosition(int position)
Sets the position of the text insertion caret for the TextComponent. Note that the caret tracks change, so this may move if the underlying text of the component is changed. If the document is null, does nothing. The position must be between 0 and the length of the component's text or else an exception is thrown.
Parameters:
position - the position
Throws:
IllegalArgumentException - if the value supplied for position is less than zero or greater than the component's text length

具体的http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#setCaretPosition(int)

------解决方案--------------------
http://www.cnblogs.com/lionden/archive/2012/10/17/swing_textarea.html