9

AJTextAreaのタブ サイズは、 を使用して簡単に設定できますsetTabSize(int)

でそれを行う同様の方法はありJEditorPaneますか?

現在、ペイン内のタブ付きのテキストは次のようになっています。

if (stuff){
            more stuff;
}

そして、私ははるかに小さいタブストップを好むでしょう:

if (stuff){
    more stuff;
}
4

3 に答える 3

12

JEdi​​torPane はさまざまな種類のコンテンツ タイプをサポートするように設計されているため、「タブ サイズ」を直接指定する方法は提供されません。その意味はコンテンツ モデルによって定義される必要があるためです。ただし、PlainDocument またはその子孫の 1 つであるモデルを使用する場合、探しているものを提供する「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";
于 2009-04-16T19:48:24.077 に答える
6

誰かが StyledDocument を使用している場合 (他の回答のリンクが死亡しました)

TabStops の配列である TabSet を作成します。私の場合、最初のタブだけを気にし、左から 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);
于 2012-04-03T17:57:31.667 に答える
0

これを理解するのにしばらく時間がかかりました。そして、フォントサイズに基づいて幅を計算したTabSetでTabStopを使用することにしました。これは、(JEditPane の paint() メソッドで) フォント サイズが変更されるたびにリセットする必要があります。

複雑なもの!:(

于 2012-10-10T21:27:48.313 に答える