【问题标题】:Java - Auto-Indentation in JTextPaneJava - JTextPane 中的自动缩进
【发布时间】:2013-04-07 21:14:48
【问题描述】:

我正在用 Java 制作一个文本编辑器,除了自动缩进之外,我拥有我需要的一切。如果他们转到新行,我将如何使缩进保持不变。我正在为我的编辑器窗口使用 JTextPane。

基本上,如果用户输入新行,我希望新行像之前一样缩进。

到目前为止,这是我的缩进代码:

注意:我的JTextPane是txt,doc部分是JTextPane的DefaultStyledDocument();

SimpleAttributeSet attributes = new SimpleAttributeSet();

TabStop[] tabStops = new TabStop[3];
tabStops[0] = new TabStop(25, TabStop.ALIGN_LEFT, TabStop.LEAD_DOTS);
tabStops[1] = new TabStop(25, TabStop.ALIGN_LEFT, TabStop.LEAD_DOTS);
tabStops[2] = new TabStop(25, TabStop.ALIGN_LEFT, TabStop.LEAD_DOTS);
tabStops[2] = new TabStop(25, TabStop.ALIGN_LEFT, TabStop.LEAD_DOTS);


TabSet tabSet = new TabSet(tabStops);
StyleConstants.setTabSet(attributes, tabSet);
doc.setParagraphAttributes(0, 0, attributes, false);

【问题讨论】:

  • 尝试调用 doc.setParagraphAttributes(0, 1, attributes, false);包括最后一段元素

标签: java swing jtextpane


【解决方案1】:

使用文档过滤器:

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class NewLineFilter extends DocumentFilter
{
    public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
        throws BadLocationException
    {
        if ("\n".equals(str))
            str = addWhiteSpace(fb.getDocument(), offs);

        super.insertString(fb, offs, str, a);
    }

    public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)
        throws BadLocationException
    {
        if ("\n".equals(str))
            str = addWhiteSpace(fb.getDocument(), offs);

        super.replace(fb, offs, length, str, a);
    }

    private String addWhiteSpace(Document doc, int offset)
        throws BadLocationException
    {
        StringBuilder whiteSpace = new StringBuilder("\n");
        Element rootElement = doc.getDefaultRootElement();
        int line = rootElement.getElementIndex( offset );
        int i = rootElement.getElement(line).getStartOffset();

        while (true)
        {
            String temp = doc.getText(i, 1);

            if (temp.equals(" ") || temp.equals("\t"))
            {
                whiteSpace.append(temp);
                i++;
            }
            else
                break;
        }

        return whiteSpace.toString();
    }

    private static void createAndShowUI()
    {
        JTextArea textArea = new JTextArea(5, 50);
        AbstractDocument doc = (AbstractDocument)textArea.getDocument();
        doc.setDocumentFilter( new NewLineFilter() );

        JFrame frame = new JFrame("NewLineFilter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new JScrollPane(textArea) );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

阅读 Implementing a Document Filter 上的 Swing 教程部分了解更多信息。

【讨论】:

  • 对不起,我不能使用它——我的 JTextPane 已经有一个 StyledDocument,当我添加过滤器时,语法突出显示由于某种原因停止工作。如果您能以任何方式提供帮助,请提供帮助。
  • 语法高亮是如何工作的?它是否也使用 DocumentFilter。如果是这样,那么您可以使用Chaining Document Filters 中建议的方法来使用多个 DocumentFilter。另一种选择是扩展 StyledDocument 并使用我提供给您的基本代码覆盖 insertString() 方法。
  • 不,它通过 AttributeSets、CharacterAttributes 工作,比较子字符串 - 但最终使用 DefaultStyledDocument。
  • 是的,我知道它使用 AttributeSets 等。但是,每当您向文档添加/删除文本时,您都需要以某种方式启动突出显示的过程。无论如何,我上面的两个建议仍然有效。
  • 这不能回答问题。为什么要手动设置文档。您是否扩展了 DefaultStyledDocument 并重写了一些方法来突出显示?如果是这样,您覆盖了哪些方法。扩展 DefaultStyledDocument 不应停止 DocumentFilter 的工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多