如何设置光标在jTextArea的某个指定位置? int pos=1;JTextArea jTextArea1=new JTextArea("test");jTextArea1.setSelectionStart(pos);jTextArea1.setSelectionEnd(pos); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 其实是这样:我在jTextArea添加键盘监听事件,只要按回车就把jTextArea中的内容取出做处理然后清空jTextArea(我用jTextArea.setText("");),可是结果老是最后的回车去不掉,即还有一个回车符号,光标也是在下一行,我用jTextArea1.setSelectionStart(0);jTextArea1.setSelectionEnd(0); 也是只在下一行(因为前面有一个回车)我估计他是一个char而不是String ,不知还有什么办法使jTextArea能够回到什么字符都没有的情况???怎么办???????????? 使用public void keyReleased(KeyEvent k) 下面这个类可以实现对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; }} 求SQL语句 JNA 讨论大开展 JAVA企业开发 如何用JAVA写只读文件 P2P技术方面的问题,请各位大侠给予指点! 如何在jlist增加一行数据,请各位赐教````急~~!!! 想用JAVA提取位图的数据,请知道的人指个路,谢谢 一个Java Applet程序的与服务器的数据交换问题 我想3月考scjp,该怎样报名阿?考试的时间是什么时候?? jdbc,我怎么找不到jdbc driver? 关于Explicit Event Enabling的问题? 给大家提个问题,jdbc和database的
jTextArea1.setSelectionStart(0);
jTextArea1.setSelectionEnd(0); 也是只在下一行(因为前面有一个回车)
我估计他是一个char而不是String ,不知还有什么办法使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;
}
}