【问题标题】:How to correctly pad text in JTextPane?如何在 JTextPane 中正确填充文本?
【发布时间】:2014-03-12 23:15:25
【问题描述】:

我正在使用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";

}

这显示了以下内容:

【问题讨论】:

    标签: java swing jtextpane


    【解决方案1】:

    您可以尝试定义默认制表位大小,就像这里描述的 http://java-sl.com/tip_default_tabstop_size.html

    或者您可以通过 setParagrahAttributes() 为段落添加自定义 TabSet(选项卡数组)

    【讨论】:

    • 你会如何使用setParagrahAttributes()
    • doc.setParagrahAttributes(0, text.length(), sas, false);和 StyleConstants.setTabSet
    【解决方案2】:

    设置自定义 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();
                }
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多