设为首页 加入收藏

TOP

java JTextArea 定位到指定行 选中某一行
2014-11-24 09:49:25 】 浏览:1513
Tags:java JTextArea 定位 指定 选中 一行

/**
* 弹出定位行对话框
*/
private void showLocationLineDialog(){

//取得总行数
int totalLineCount = jTextArea1.getLineCount();
if(totalLineCount <= 1){
return ;
}
String title = "跳转至行:(1..."+totalLineCount+")";
String line = JOptionPane.showInputDialog(this,title);
if(line==null||"".equals(line.trim())){
return;
}
try {
int intLine = Integer.parseInt(line);
if(intLine > totalLineCount){
return;
}
//JTextArea起始行号是0,所以此处做减一处理
int selectionStart = jTextArea1.getLineStartOffset(intLine-1);
int selectionEnd = jTextArea1.getLineEndOffset(intLine-1);

//如果是不是最后一行,selectionEnd做减一处理,是为了使光标与选中行在同一行
if(intLine != totalLineCount){
selectionEnd--;

}

jTextArea1.requestFocus(); //获得焦点


jTextArea1.setSelectionStart(selectionStart);
jTextArea1.setSelectionEnd(selectionEnd);
} catch (Exception e) {
e.printStackTrace();
}
}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇内存对齐与ANSI C中struct型数据.. 下一篇Java获取*路径

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目