0

Swing アプリケーションの初心者です。text に firstLineIndent を追加する必要がありますが、この値を負に設定すると、段落の最初の行が少しぼやけて見えます。StyleConstants.setFirstLineIndent(attr,-50) メソッドの動作を認識できません。このエラーを修正する方法。

以下は、参照として使用しているコードのリンクです。

http://java-sl.com/tip_hanging_first_line.html

ありがとう...

4

1 に答える 1

1

その例が JEditorPane を拡張する理由がわかりません。エディタ ペインは HTML 用です。スタイル付きテキストには JTextPane を使用することを好みます。また、カスタム エディター キットを使用する理由もわかりません。

次のコードは私にとってはうまくいきます:

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;

//public class HangingIndent extends JEditorPane {
public class HangingIndent extends JTextPane {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Negative (Hanging) first line indent");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final HangingIndent app = new HangingIndent();
//        app.setEditorKit(new MyEditorKit());
        app.setText("The paragraph with long text is necessary to show how " +
                "hanging indent can be achieved. We should set not only the " +
                "first line indent but the same left indent.");
        StyledDocument doc=(StyledDocument)app.getDocument();
        SimpleAttributeSet attrs=new SimpleAttributeSet();
        StyleConstants.setFirstLineIndent(attrs, -50);
        StyleConstants.setLeftIndent(attrs, 50);

        doc.setParagraphAttributes(0,doc.getLength(),attrs, false);

        JScrollPane scroll = new JScrollPane(app);
        frame.getContentPane().add(scroll);

        frame.setSize(400, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public HangingIndent() {
        super();
    }
}
于 2013-07-17T14:37:33.193 に答える