2015-11-05 132 views
2

我想将JTextPane中的制表符\ t的大小设置为4个空格。Java在JTextPane上设置缩进大小

谷歌搜索后,我发现了一些东西,我会包括在这里为我所尝试过的,也许为什么他们失败了。

How do you set the tab size in a JEditorPane?

JTextPane不是一个普通的文件。

Java JTextpane Tab Size

Eclipse中提出了一些错误:

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。代码我改编自它并使它:

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

* “我想在'JTextPane'设置缩进大小为4。” * 4是什么?像素,人物,..情节? –

+1

我不清楚你用'indent'表示你的意思。这仅仅是TextPane边缘与实际文本之间的空间吗?如果是这样,只需将其边框设置为适当大小的空白边框。 – FredK

+0

我不明白“缩进大小”对您意味着什么。通常缩进是从左边缘开始的空间,并且您不解释为什么'setLeftIndent(...)'方法不起作用。我会认为这是你应该使用的解决方案。但是接下来你还要讨论tabbing,它基于文本窗格的数据,而不是一行文本的起始缩进。 – camickr

回答

2

我想设置一个制表符,\吨的大小,在一个的JTextPane为4位宽。

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中的默认字体的空间的宽度时,是不是很宽,因此实际的选项卡将不会那么大。

+0

其实正常选项卡的大小太大了。它大约有10个空间。 –

+0

如果宽度比较大5 TabStops是不够的。也取决于字体。 – StanislavL

+0

'5 TabStops是不够的。 - 是的,很容易将阵列大小更改为更真实的值。 – camickr

1

从链接http://java-sl.com/tip_default_tabstop_size.html

import javax.swing.text.*; 
import javax.swing.*; 

public class TabSizeEditorKit extends StyledEditorKit { 

    public static final int TAB_SIZE=36; 

    public ViewFactory getViewFactory() { 
     return new MyViewFactory(); 
    } 

    static class MyViewFactory implements ViewFactory { 

     public View create(Element elem) { 
      String kind = elem.getName(); 
      if (kind != null) { 
       if (kind.equals(AbstractDocument.ContentElementName)) { 
        return new LabelView(elem); 
       } else if (kind.equals(AbstractDocument.ParagraphElementName)) { 
        return new CustomTabParagraphView(elem); 
       } else if (kind.equals(AbstractDocument.SectionElementName)) { 
        return new BoxView(elem, View.Y_AXIS); 
       } else if (kind.equals(StyleConstants.ComponentElementName)) { 
        return new ComponentView(elem); 
       } else if (kind.equals(StyleConstants.IconElementName)) { 
        return new IconView(elem); 
       } 
      } 

      return new LabelView(elem); 
     } 
    } 

    public static void main(String[] args) { 
     JFrame frame=new JFrame("Custom default Tab Size in EditorKit example"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JEditorPane edit=new JEditorPane(); 
     edit.setEditorKit(new TabSizeEditorKit()); 
     try { 
      edit.getDocument().insertString(0,"1\t2\t3\t4\t5", new SimpleAttributeSet()); 
     } catch (BadLocationException e) { 
      e.printStackTrace(); 
     } 
     frame.getContentPane().add(new JScrollPane(edit)); 

     frame.setSize(300,100); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 


    static class CustomTabParagraphView extends ParagraphView { 

     public CustomTabParagraphView(Element elem) { 
      super(elem); 
     } 

     public float nextTabStop(float x, int tabOffset) { 
      TabSet tabs = getTabSet(); 
      if(tabs == null) { 
       // a tab every 72 pixels. 
       return (float)(getTabBase() + (((int)x/TAB_SIZE + 1) * TAB_SIZE)); 
      } 

      return super.nextTabStop(x, tabOffset); 
     } 

    } 
}