您可以在JTextPane上设置javax.swing.text.document。以下示例将使您了解我的意思:)
import java.awt.Dimension;import javax.swing.Jframe;import javax.swing.JTextPane;import javax.swing.text.AttributeSet;import javax.swing.text.BadLocationException;import javax.swing.text.DefaultStyleddocument;public class Tester { public static void main(String[] args) { JTextPane textpane = new JTextPane(); textpane.setdocument(new Tabdocument()); Jframe frame = new Jframe(); frame.getContentPane().add(textpane); frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); frame.setSize(new Dimension(200, 200)); frame.setVisible(true); } static class Tabdocument extends DefaultStyleddocument { @Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { str = str.replaceAll("t", " "); super.insertString(offs, str, a); } }}
定义一个DefaultStyledocument来完成这项工作。然后将document设置为您的JTextPane。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)