JTextPane - 使Caret正常尺寸?

时间:2012-08-04 14:04:38

标签: java swing cursor jtextpane

所以我试图让JTextPane中的文本双倍间隔。这是我的代码:

     MutableAttributeSet attrs = editor.getInputAttributes();
     StyleConstants.setLineSpacing(attrs, 2);
     editor.getStyledDocument().setParagraphAttributes(0, doc.getLength() + 1, attrs, true);

问题在于,光标(插入符号)大到三到四行空间。如何将插入符号调整到正常大小?

这是一个屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:2)

试试这个:

editor.setCaret(new DefaultCaret() {

    public void paint(Graphics g) {

        JTextComponent comp = getComponent();
        if (comp == null)
            return;

        Rectangle r = null;
        try {
            r = comp.modelToView(getDot());
            if (r == null)
                return;
        } catch (BadLocationException e) {
            return;
        }
        r.height = 15; //this value changes the caret size
        if (isVisible())
            g.fillRect(r.x, r.y, 1, r.height);
    }
});