在Swing的JTextPane中设置标签策略

在Swing的JTextPane中设置标签策略,第1张

在Swing的JTextPane中设置标签策略

您可以在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。

干杯



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5462239.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存