最近我一直在做一个Java文本编辑器项目,我想用aJTextPane代替旧的JTextArea以实现语法高亮。但是, aJTextPane中缺少方法JTextArea(例如append()等getLineStartOffset()),我想在我的类MyTextPane( 的子类JTextPane)中重新实现它们,但遇到了麻烦。
我当前的代码(只有一小部分独立部分):
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class MyTextPane extends JTextPane
{
public MyTextPane()
{
super();
}
public void append(String text)
{
try
{
Document doc = this.getDocument();
doc.insertString(doc.getLength(),text,null);
}
catch (BadLocationException ex)
{
//must succeed
throw new InternalError(ex.getMessage());
}
}
public void insert(String text, int pos)
{
try
{
this.getStyledDocument().insertString(pos,text,null);
}
catch (BadLocationException ex)
{
throw new IllegalArgumentException(ex);
} …Run Code Online (Sandbox Code Playgroud)