简体   繁体   English

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

[英]Java JTextPane changes line height when UTF8 character is added

I am using Java JDK 1.6 and have a problem using JTextPane to show text with a monospaced font.我正在使用 Java JDK 1.6,在使用 JTextPane 显示等宽字体文本时遇到问题。 As soon as I add a UTF8-character like, the line height in the textpane is reduced (for all the text already in the pane and also all text added later).一旦我添加了一个类似 UTF8 的字符,文本窗格中的行高就会降低(对于窗格中已经存在的所有文本以及稍后添加的所有文本)。 How can I avoid this?我怎样才能避免这种情况? I would like to have the normal line height.我想要正常的行高。

Here is some sample code:这是一些示例代码:

    class AttributedTextPane extends JTextPane
    {

        private DefaultStyledDocument defaultStyledDocument;

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

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

This pane is integrated into an JInternalFrame.此窗格集成到 JInternalFrame 中。 Creating the panel and setting the desired monospaced font:创建面板并设置所需的等宽字体:

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

To display the desired text, I call pane.setText(...);要显示所需的文本,我调用 pane.setText(...); As soon as I add the UTF8 character, the line height changes, see screenshot at http://i.imgur.com/Fq7XBJB.png .一旦我添加了 UTF8 字符,行高就会发生变化,请参阅http://i.imgur.com/Fq7XBJB.png的屏幕截图。 Is there a way to avoid that the line height is changed?有没有办法避免改变行高? Thanks, Deejay谢谢,播音员

Old question but I have been struggling with it for some time, though with a JTextArea.老问题,但我已经为它苦苦挣扎了一段时间,尽管使用了 JTextArea。 The solution is to have either a VM param -Di18n=true or put the i18n property in the document.解决方案是使用 VM 参数 -Di18n=true 或将 i18n 属性放入文档中。

My test code:我的测试代码:

import javax.swing.JTextArea;

public class Test {
    public static void main(String[] argv) {
        JTextArea ta = new JTextArea();
        //ta.getDocument().putProperty("i18n", Boolean.TRUE);
        ta.setText("A");
        System.out.println(ta.getPreferredSize()); // - height 16 without i18n and using default font, 15 with i18n
        ta.setText("\ud8ff\udc05"); // surrogate pair
        System.out.println(ta.getPreferredSize()); // - height 15
        ta.setText("A");
        System.out.println(ta.getPreferredSize()); // - height 15
    }
}

When i18n is not enabled and the document is appended, the elements that are created are PlainView/WrappedPlainView that return the height based on the FontMetrics height.当未启用 i18n 并附加文档时,创建的元素是 PlainView/WrappedPlainView,它们返回基于 FontMetrics 高度的高度。

When i18n is enabled the elements are PlainParagraph that contain GlyphView that calculates the height differently.当启用 i18n 时,元素是 PlainParagraph,其中包含以不同方式计算高度的 GlyphView。

When i18n is not enabled and the document is appended with a surrogate pair then due to SwingUtilities2.isComplexLayout returning true, the i18n property is automatically set to true for the document and then all elements are created as PlainParagraph containing GlyphView that return the different (always smaller?) height.当未启用 i18n 并且文档附加了代理项对时,由于 SwingUtilities2.isComplexLayout 返回 true,文档的 i18n 属性自动设置为 true,然后所有元素都创建为包含 GlyphView 的 PlainParagraph,返回不同的(总是更小?)高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM