JEdi​​torPane - 制作制表符空格

发布于 2024-09-06 10:39:14 字数 184 浏览 2 评论 0原文

我想知道是否有一种方法可以将 jeditorpane 中的制表符转换为空格,就像您在 IDE 中工作时看到的那样。我不想设置选项卡大小。我已经可以轻松做到这一点了。

我想将制表符替换为空格中的等效项。例如,如果我的选项卡长度为 5 个空格,我希望所有选项卡在创建时立即替换为 5 个空格。

有什么想法吗?

I'm wondering if there's a way to have tabs convert to spaces in a jeditorpane, much like you'd see when working in an IDE. I don't want to set the tab size. I can already do that easily.

I want to have tabs replaced with their equivalent in spaces. So for example if my tabs are 5 spaces long, I would want all tabs replaced immediately with 5 spaces whenever they are created.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间不值得 2024-09-13 10:39:14

DocumentFilter 添加到 AbstractDocument,以便在文本插入到 Document 时用空格替换制表符。

阅读 Swing 教程中有关文本组件功能 了解更多信息。

简单的例子:

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

public class TabToSpaceFilter extends DocumentFilter
{
    @Override
    public void insertString(FilterBypass fb, int offset, String text, AttributeSet attributes)
        throws BadLocationException
    {
        replace(fb, offset, 0, text, attributes);
    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attributes)
        throws BadLocationException
    {
        //  In case someone tries to clear the Document by using setText(null)

        if (text == null)
            text = "";

        super.replace(fb, offset, length, text.replace("\t", "    "), attributes);
    }

    private static void createAndShowGUI()
    {
        JTextArea textArea = new JTextArea(5, 20);
        AbstractDocument doc = (AbstractDocument) textArea.getDocument();
        doc.setDocumentFilter( new TabToSpaceFilter() );

        JFrame frame = new JFrame("Integer Filter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout( new java.awt.GridBagLayout() );
        frame.add( new JScrollPane(textArea) );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
    }

}

Add a DocumentFilter to the AbstractDocument to replaces tabs with spaces as text is inserted into the Document.

Read the section from the Swing tutorial on Text Component Features for more information.

Simple example:

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

public class TabToSpaceFilter extends DocumentFilter
{
    @Override
    public void insertString(FilterBypass fb, int offset, String text, AttributeSet attributes)
        throws BadLocationException
    {
        replace(fb, offset, 0, text, attributes);
    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attributes)
        throws BadLocationException
    {
        //  In case someone tries to clear the Document by using setText(null)

        if (text == null)
            text = "";

        super.replace(fb, offset, length, text.replace("\t", "    "), attributes);
    }

    private static void createAndShowGUI()
    {
        JTextArea textArea = new JTextArea(5, 20);
        AbstractDocument doc = (AbstractDocument) textArea.getDocument();
        doc.setDocumentFilter( new TabToSpaceFilter() );

        JFrame frame = new JFrame("Integer Filter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout( new java.awt.GridBagLayout() );
        frame.add( new JScrollPane(textArea) );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文