如何在 JEditorPane 中设置选项卡大小?

发布于 2024-07-17 17:24:56 字数 306 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妞丶爷亲个 2024-07-24 17:24:56

由于 JEditorPane 旨在支持不同类型的内容类型,因此它不提供直接指定“选项卡大小”的方法,因为其含义应由内容模型定义。
但是,当您使用 PlainDocument 或其后代之一的模型时,有一个“tabSizeAttribute”可以提供您正在寻找的内容。

示例:

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";

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:

JEditorPane pane = new JEditorPane(...);
...
Document doc = pane.getDocument();
if (doc instanceof PlainDocument) {
    doc.putProperty(PlainDocument.tabSizeAttribute, 8);
}
...

From the 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";
森林迷了鹿 2024-07-24 17:24:56

如果有人使用 StyledDocument (另一个答案上的链接已失效),

您可以创建一个 TabSet,它是 TabStop 的数组。 就我而言,我只关心第一个选项卡,并且我希望它距离左侧 20px,所以这段代码对我有用:

StyleContext sc = StyleContext.getDefaultStyleContext();
TabSet tabs = new TabSet(new TabStop[] { new TabStop(20) });
AttributeSet paraSet = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabs);
pane.setParagraphAttributes(paraSet, false);

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:

StyleContext sc = StyleContext.getDefaultStyleContext();
TabSet tabs = new TabSet(new TabStop[] { new TabStop(20) });
AttributeSet paraSet = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabs);
pane.setParagraphAttributes(paraSet, false);
旧故 2024-07-24 17:24:56

我花了一段时间才弄清楚这一点。
并决定在 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! :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文