Java 如何在JtextPane中设置行距?

Java 如何在JtextPane中设置行距?,java,swing,line,jtextpane,spacing,Java,Swing,Line,Jtextpane,Spacing,首先,我将JTextPane设置为: HTMLEditorKit editorKit = new HTMLEditorKit(); HTMLDocument document = (HTMLDocument) editorKit.createDefaultDocument(); JTextPane textPane = new JTextPane(); textPane.setContentType("text/html"); textPane.setDocument(document); 我

首先,我将JTextPane设置为:

HTMLEditorKit editorKit = new HTMLEditorKit();
HTMLDocument document = (HTMLDocument) editorKit.createDefaultDocument();
JTextPane textPane = new JTextPane();
textPane.setContentType("text/html");
textPane.setDocument(document);
我想在JtextPane中设置行距,这是我的想法,但它无法工作:

SimpleAttributeSet aSet = new SimpleAttributeSet();
StyleConstants.setLineSpacing(aSet, 50);
textPane.setParagraphAttributes(aSet, false);

我错了?

要设置JTextPane的样式,可以使用样式表:


要设置JTextPane的样式,可以使用样式表:


调用
textPane.setParagraphAttributes(aSet,false)时
它尝试将行距应用于选择,但未选择任何内容

换一种说法

document.setParagraphAttributes(0, document.getLength(), attr, replace);

调用
textPane.setParagraphAttributes(aSet,false)时
它尝试将行距应用于选择,但未选择任何内容

换一种说法

document.setParagraphAttributes(0, document.getLength(), attr, replace);

我一直在努力解决这个问题,然后在方法
public void setParagraphAttributes(AttributeSet attr,boolean replace)
的API中,我发现:

如果存在选择,则属性将应用于与选择相交的段落。如果没有选择,则属性将应用于当前插入符号位置的段落

因此,OP的方法将起作用,但您必须应用
textPane。在设置行距之前选择all()
您只需执行一次,并且附加到此
JTextPane
的所有文本将具有相同的行距,即使设置行距时窗格中可能没有文本。我在实例化时也会这样做

因此,适用于我的代码是:

/**
 * Select all the text of a <code>JTextPane</code> first and then set the line spacing.
 * @param the <code>JTextPane</code> to apply the change
 * @param factor the factor of line spacing. For example, <code>1.0f</code>.
 * @param replace whether the new <code>AttributeSet</code> should replace the old set. If set to <code>false</code>, will merge with the old one.
 */
private void changeLineSpacing(JTextPane pane, float factor, boolean replace) {
    pane.selectAll();
    MutableAttributeSet set = new SimpleAttributeSet(pane.getParagraphAttributes());
    StyleConstants.setLineSpacing(set, factor);
    txtAtributosImpresora.setParagraphAttributes(set, replace);
}
注意:它将用因子*(文本的行高)替换当前行距,而不是因子*原始行距。真奇怪

如果
JTextPane
位于
JScrollPane
中,并且文本长度太长,它将滚动到底部。通常我们想看顶部。要重置滚动的位置,最后您可以添加:

pane.setCaretPosition(0); //scroll to the top at last.
注:为了设置段落边距,我们有:

textPane.setMargin(new Insets(10, 5, 10, 5)); //top, left, bottom, right

我一直在努力解决这个问题,然后在方法
public void setParagraphAttributes(AttributeSet attr,boolean replace)
的API中,我发现:

如果存在选择,则属性将应用于与选择相交的段落。如果没有选择,则属性将应用于当前插入符号位置的段落

因此,OP的方法将起作用,但您必须应用
textPane。在设置行距之前选择all()
您只需执行一次,并且附加到此
JTextPane
的所有文本将具有相同的行距,即使设置行距时窗格中可能没有文本。我在实例化时也会这样做

因此,适用于我的代码是:

/**
 * Select all the text of a <code>JTextPane</code> first and then set the line spacing.
 * @param the <code>JTextPane</code> to apply the change
 * @param factor the factor of line spacing. For example, <code>1.0f</code>.
 * @param replace whether the new <code>AttributeSet</code> should replace the old set. If set to <code>false</code>, will merge with the old one.
 */
private void changeLineSpacing(JTextPane pane, float factor, boolean replace) {
    pane.selectAll();
    MutableAttributeSet set = new SimpleAttributeSet(pane.getParagraphAttributes());
    StyleConstants.setLineSpacing(set, factor);
    txtAtributosImpresora.setParagraphAttributes(set, replace);
}
注意:它将用因子*(文本的行高)替换当前行距,而不是因子*原始行距。真奇怪

如果
JTextPane
位于
JScrollPane
中,并且文本长度太长,它将滚动到底部。通常我们想看顶部。要重置滚动的位置,最后您可以添加:

pane.setCaretPosition(0); //scroll to the top at last.
注:为了设置段落边距,我们有:

textPane.setMargin(new Insets(10, 5, 10, 5)); //top, left, bottom, right

@这不是我想回答的问题。我希望是在运行时可以设置的,并且可以通过textPane.getText()获取HTML代码。@Aleksei Bulgak这不是我想要回答的问题。我想是在运行时可以设置的,并且可以通过textPane.getText()获取HTML代码。仍然没有更改。StyledDocument文档=textWeight.getStyledDocument();可变属性集=新的SimpleAttributeSet();StyleConstants.setLineSpacing(集合,50);document.setParagraphAttributes(0,document.getLength(),set,false);还是没有变化。StyledDocument文档=textWeight.getStyledDocument();可变属性集=新的SimpleAttributeSet();StyleConstants.setLineSpacing(集合,50);document.setParagraphAttributes(0,document.getLength(),set,false);