簡體   English   中英

如何在JTextPane中正確填充文本?

[英]How to correctly pad text in JTextPane?

我正在使用JTexPane來顯示一些文本,其中包含需要突出顯示的部分。 我如何正確填充空格?

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class test2 {

    public static void main(String[] args) {
        final JTextPane textPane = new JTextPane();
        textPane.setContentType("text/html");
        textPane.setText("<html>" + text.replaceAll("\\n", "<br/>").replaceAll(" ", "&nbsp;").replaceAll("\\t", "&nbsp;&nbsp;") + "</html>");
        StyledDocument doc = textPane.getStyledDocument();
        SimpleAttributeSet sas = new SimpleAttributeSet();
        StyleConstants.setForeground(sas, Color.blue);
        doc.setCharacterAttributes(0, text.length(), sas, false);
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Test");
                frame.setPreferredSize(new Dimension(900, 800));
                frame.getContentPane().add(textPane);
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }


    static String text = "\n" +
            "\n" +
            " pop12 2014-01-12         just a padded text\n" +
            "                          line 2\n" +
            "                          line 3                            \n";

}

這顯示以下內容:

在此輸入圖像描述

您可以嘗試定義默認選項卡停止大小,如此處所述http://java-sl.com/tip_default_tabstop_size.html

或者您可以通過setParagrahAttributes()為段落添加自定義TabSet(選項卡數組)

設置自定義TabSet的示例:

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

public class TextPaneTabs
{
    public static void setTabs( JTextPane textPane, int charactersPerTab)
    {
        FontMetrics fm = textPane.getFontMetrics( textPane.getFont() );
        int charWidth = fm.charWidth( 'w' );
        int tabWidth = charWidth * charactersPerTab;
//      int tabWidth = 100;

        TabStop[] tabs = new TabStop[5];

        for (int j = 0; j < tabs.length; j++)
        {
            int tab = j + 1;
            tabs[j] = new TabStop( tab * tabWidth );
        }

        TabSet tabSet = new TabSet(tabs);
        SimpleAttributeSet attributes = new SimpleAttributeSet();
        StyleConstants.setTabSet(attributes, tabSet);
        int length = textPane.getDocument().getLength();
        textPane.getStyledDocument().setParagraphAttributes(0, length, attributes, false);
    }

    private static void createAndShowUI()
    {
        JTextPane textPane = new JTextPane();
        textPane.setText("12345678\n\t1\t2\t3aaaaa\t4\t5\t6\t7\t8\n\t1\t2\t3\t4\t5\t6\t7\t8\n\t\t12345678");
        JScrollPane scrollPane = new JScrollPane( textPane );
        scrollPane.setPreferredSize( new Dimension(700, 100 ) );

        // Change the tab size to 4 characters

        setTabs( textPane, 8 );

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( scrollPane );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM