Java在JTextPane上设置缩进大小 [英] Java Setting Indent Size on JTextPane

查看:362
本文介绍了Java在JTextPane上设置缩进大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将JTextPane中制表符\ t的大小设置为4个空格.

I want to set the size of a tab character, \t, in a JTextPane to be 4 spaces wide.

在谷歌搜索了很多之后,我发现了一些我要尝试的东西以及它们为什么失败的东西.

After Googling quite a bit I found some things that I will include here for what I have tried and maybe why they failed.

如何在JEdi​​torPane?

JTextPane不是普通文档.

Java JTextpane标签大小

Eclipse引发了一些错误:

Eclipse raised some errors:

Type mismatch: cannot convert from javax.swing.text.AttributeSet to 
 javax.print.attribute.AttributeSet

The method setParagraphAttributes(javax.swing.text.AttributeSet, boolean) in the type JTextPane is not applicable for the 
 arguments (javax.print.attribute.AttributeSet, boolean)

http://www.java2s.com/Code/Java /Swing-JFC/TextPaneSample.htm

此页面讨论使用JTextPane进行样式设置.我改编而成的代码如下:

This page talks about styling with JTextPane. The code I adapted from it and made this:

MutableAttributeSet set = new SimpleAttributeSet(pane.getParagraphAttributes());
StyleConstants.setLeftIndent(set, 40);
StyleConstants.setRightIndent(set, 40);

推荐答案

我想将JTextPane中制表符\ t的大小设置为4个空格.

I want to set the size of a tab character, \t, in a JTextPane to be 4 spaces wide.

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

public class TextPaneTabs
{
    public static void setTabs( final JTextPane textPane, int charactersPerTab)
    {
        FontMetrics fm = textPane.getFontMetrics( textPane.getFont() );
//          int charWidth = fm.charWidth( 'w' );
        int charWidth = fm.charWidth( ' ' );
        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, 4 );

        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();
            }
        });
    }
}

当然,当使用JTextPane的默认Font时,空格的宽度不是很宽,因此实际选项卡不会那么大.

Of course when using the default Font of a JTextPane the width of a space is not very wide so the actual tab will not be that big.

这篇关于Java在JTextPane上设置缩进大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆