首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JTextPane中的Java自动缩进

JTextPane中的Java自动缩进
EN

Stack Overflow用户
提问于 2013-04-07 21:14:48
回答 1查看 2K关注 0票数 4

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

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

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

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

代码语言:javascript
运行
复制
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);
EN

Stack Overflow用户

回答已采纳

发布于 2013-04-07 23:56:05

使用文档筛选器:

代码语言:javascript
运行
复制
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();
            }
        });
    }
}

有关更多信息,请阅读有关实现文档筛选器的Swing教程中的部分。

票数 4
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15867900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档