如何在JEditorPane中设置标签大小? [英] How do you set the tab size in a JEditorPane?
问题描述
JTextArea
的标签大小可以使用 setTabSize(int)
轻松设置。
A JTextArea
's tab size can easily be set using setTabSize(int)
.
是否有类似的方法使用 JEditorPane
?
Is there a similar way to do it with a JEditorPane
?
现在,我的窗格中带有标签的文字如下所示:
Right now, text with tabs in my pane looks like:
if (stuff){
more stuff;
}
而且,我更喜欢一个小得多的制表位:
And, I'd prefer a much smaller tab stop:
if (stuff){
more stuff;
}
推荐答案
由于JEditorPane旨在支持不同种类的内容类型,它没有提供直接指定选项卡大小的方法,因为它的含义应该由内容模型定义。
但是当你使用一个PlainDocument模型或它的一个后代时,有一个tabSizeAttribute可以提供你想要的东西。
As JEditorPane is designed to support different kinds of content types, it does not provide a way to specify a "tab size" directly, because the meaning of that should be defined by the content model. However when you use a model that's a PlainDocument or one of its descendants, there is a "tabSizeAttribute" that provides what you are looking for.
示例:
JEditorPane pane = new JEditorPane(...);
...
Document doc = pane.getDocument();
if (doc instanceof PlainDocument) {
doc.putProperty(PlainDocument.tabSizeAttribute, 8);
}
...
来自Javadoc:
/**
* Name of the attribute that specifies the tab
* size for tabs contained in the content. The
* type for the value is Integer.
*/
public static final String tabSizeAttribute = "tabSize";
这篇关于如何在JEditorPane中设置标签大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!