int pos=1;
JTextArea jTextArea1=new JTextArea("test");
jTextArea1.setSelectionStart(pos);
jTextArea1.setSelectionEnd(pos);

解决方案 »

  1.   

    其实是这样:我在jTextArea添加键盘监听事件,只要按回车就把jTextArea中的内容取出做处理然后清空jTextArea(我用jTextArea.setText("");),可是结果老是最后的回车去不掉,即还有一个回车符号,光标也是在下一行,我用
    jTextArea1.setSelectionStart(0);
    jTextArea1.setSelectionEnd(0); 也是只在下一行(因为前面有一个回车)
    我估计他是一个char而不是String ,不知还有什么办法使jTextArea能够回到什么字符都没有的情况???怎么办????????????
      

  2.   

    使用public void keyReleased(KeyEvent k) 
      

  3.   

    下面这个类可以实现对JTextArea进行行操作的功能
    import javax.swing.JTextArea;
    import javax.swing.text.BadLocationException;/**
    * <p>Title:The JTextArea support to deletLine,getLineText,setLineText and insertLine </p>
    * <p>Description: </p>
    * <p>Copyright: Copyright (c) 2002</p>
    * <p>Company: </p>
    * @author z_yheart(年轻的心)
    * @version 1.0
    */public class MyTextArea extends JTextArea {  public MyTextArea() {
      }
      public void deleteLine(int Line) throws BadLocationException
      {
        int start=0;
        int end=0;
        try{
        start=this.getLineStartOffset(Line);
        end=this.getLineEndOffset(Line);
        }
        catch(BadLocationException ble)
        {
          throw ble;
        }
        this.replaceRange("",start,end);  }
      public void insertLine(String str,int Line) throws BadLocationException
      {
        int start =0;
        int end =0;    try{        start=this.getLineStartOffset(Line);    }
        catch(BadLocationException ble)
        {
          throw ble;
        }
        this.insert(str+"\r\n",start);
      }
      public void setLineText(String str,int Line) throws BadLocationException
      {
        int start =0;
        int end =0;  try{
        start=this.getLineStartOffset(Line);
        end=this.getLineEndOffset(Line);
        }
        catch(BadLocationException ble)
        {
          throw ble;
        }
        this.replaceRange("",start,end-1);
        this.insert(str+"\r\n",start);
      }
      public String getLineText(int Line) throws BadLocationException
      {
        int start =0;
        int end =0;
        String str="";
        try{
        start=this.getLineStartOffset(Line);
        end=this.getLineEndOffset(Line);
      str=this.getText(start,end);
        }
        catch(BadLocationException ble)
        {
          throw ble;
        }
        return str;
      }