当前位置: 技术问答>java相关
我的Swing问题!
来源: 互联网 发布时间:2015-03-20
本文导语: 我想对JTextArea的text以行为单位进行操作,例如删除等,我该怎么办? 请大家帮我! | 下面这个类可以实现你的功能 import javax.swing.JTextArea; import javax.swing.text.BadLocationException; /** * Title:The...
我想对JTextArea的text以行为单位进行操作,例如删除等,我该怎么办?
请大家帮我!
请大家帮我!
|
下面这个类可以实现你的功能
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
/**
*
*
*
*
* @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+"rn",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+"rn",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;
}
}
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
/**
*
Title:The JTextArea support to deletLine,getLineText,setLineText and insertLine
*
Description:
*
Copyright: Copyright (c) 2002
*
Company:
* @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+"rn",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+"rn",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;
}
}
|
使用JTextArea的以下方法进行处理
int getLineCount()
Determines the number of lines contained in the area.
int getLineEndOffset(int line)
Determines the offset of the end of the given line.
int getLineOfOffset(int offset)
Translates an offset into the components text to a line number.
int getLineStartOffset(int line)
Determines the offset of the start of the given line.
int getLineCount()
Determines the number of lines contained in the area.
int getLineEndOffset(int line)
Determines the offset of the end of the given line.
int getLineOfOffset(int offset)
Translates an offset into the components text to a line number.
int getLineStartOffset(int line)
Determines the offset of the start of the given line.
|
一个简单的方法:
把 JTextArea的text 读到一个字符串中,然后再编写对字符串的处理类,就可以了,
把 JTextArea的text 读到一个字符串中,然后再编写对字符串的处理类,就可以了,