gpt4 book ai didi

添加UTF8字符时Java JTextPane更改行高

转载 作者:太空宇宙 更新时间:2023-11-04 12:08:49 24 4
gpt4 key购买 nike

我使用的是 Java JDK 1.6,并且在使用 JTextPane 显示等宽字体的文本时遇到问题。一旦我添加像 😂 这样的 UTF8 字符,文本 Pane 中的行高就会减小(对于 Pane 中已有的所有文本以及稍后添加的所有文本)。我怎样才能避免这种情况?我想要正常的行高。

这里是一些示例代码:

    class AttributedTextPane extends JTextPane
{

private DefaultStyledDocument defaultStyledDocument;

protected AttributedTextPane()
{
this.defaultStyledDocument = new DefaultStyledDocument();
this.setDocument(defaultStyledDocument);

this.setContentType("text/plain");
...
}
}
...

此 Pane 集成到 JInternalFrame 中。创建面板并设置所需的等宽字体:

    Font font = new Font("DejaVu Sans Mono", Font.PLAIN, 11);
AttributedTextPane pane = new AttributedTextPane();
pane.setFont(font);

为了显示所需的文本,我调用pane.setText(...);一旦我添加UTF8字符,行高就会改变,请参见/image/hBZDe.png处的屏幕截图。有没有办法避免行高改变?谢谢,DJ

最佳答案

您可以尝试设置/强制行高,如下所示:

MutableAttributeSet jTextPaneSet = new SimpleAttributeSet(pane.getParagraphAttributes());
StyleConstants.setLineSpacing(jTextPaneSet, 1.5f); //replace float 1.5f with your desired line spacing/height

来源:

http://docs.oracle.com/javase/8/docs/api/javax/swing/JTextPane.html#setParagraphAttributes(javax.swing.text.AttributeSet,%20boolean)

https://docs.oracle.com/javase/7/docs/api/javax/swing/text/StyleConstants.html#setLineSpacing(javax.swing.text.MutableAttributeSet,%20float)

关于添加UTF8字符时Java JTextPane更改行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40068765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com