如何在 JEditorPane 中设置选项卡大小?
JTextArea
的选项卡大小可以使用 setTabSize(int)
轻松设置。
是否有类似的方法可以使用 JEditorPane
来完成此操作?
现在,我的窗格中带有选项卡的文本如下所示:
if (stuff){
more stuff;
}
而且,我更喜欢更小的制表位:
if (stuff){
more stuff;
}
A JTextArea
's tab size can easily be set using setTabSize(int)
.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于 JEditorPane 旨在支持不同类型的内容类型,因此它不提供直接指定“选项卡大小”的方法,因为其含义应由内容模型定义。
但是,当您使用 PlainDocument 或其后代之一的模型时,有一个“tabSizeAttribute”可以提供您正在寻找的内容。
示例:
来自 Javadoc:
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.
Example:
From the Javadoc:
如果有人使用 StyledDocument (另一个答案上的链接已失效),
您可以创建一个 TabSet,它是 TabStop 的数组。 就我而言,我只关心第一个选项卡,并且我希望它距离左侧 20px,所以这段代码对我有用:
In case anyone's using a StyledDocument (The link on the other answer died)
You create a TabSet which is an array of TabStops. In my case I only cared about the 1st tab, and I wanted it 20px from the left, so this code worked for me:
我花了一段时间才弄清楚这一点。
并决定在 TabSet 中使用 TabStop,它根据字体大小计算宽度。
当字体大小发生变化时(在 JEditPane 的 Paint() 方法中),必须重置它。
复杂的东西! :(
Took me a while to figure this out.
And decided to use TabStop's in a TabSet that have calculated width based on the font size.
This has to be reset when ever the font size changes (in the paint() method of the JEditPane).
Complicated stuff! :(