java - 带有 Java 7 的 JTextPane 中带有样式文本的奇怪文本换行

标签 java swing word-wrap java-7

我有两个不同的编辑器使用 JTextPane,在 Java 7 中有奇怪的错误,这些错误在以前的 JVM 版本中没有出现。它发生在包含样式文本或组件的长行中。

这是一个演示此错误的示例。在此示例中,每次插入一个字符时,默认样式将应用于所有文本。我使用 JDK 1.7.0_04 对其进行了测试。

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

public class BugWrapJava7 extends JFrame {

    JTextPane jtp;
    StyledDocument doc;

    public BugWrapJava7() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        jtp = new JTextPane();
        add(jtp, BorderLayout.CENTER);
        jtp.setText("\ntype some text in the above empty line and check the wrapping behavior");
        doc = jtp.getStyledDocument();
        doc.addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                insert();
            }
            public void removeUpdate(DocumentEvent e) {
            }
            public void changedUpdate(DocumentEvent e) {
            }
        });
        setSize(200, 200);
        setVisible(true);
    }
    public void insert() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Style defaultStyle = jtp.getStyle(StyleContext.DEFAULT_STYLE);
                doc.setCharacterAttributes(0, doc.getLength(), defaultStyle, false);
            }
        });
    }
    public static void main(String[] args) {
        new BugWrapJava7();
    }
}

我的问题是:我的代码是否有问题,或者它确实是 Java 7 中引入的新错误?如果这是一个新的 JVM 错误,是否有解决方法?

可能与question 8666727有关, 但这里的问题在于错误的包装而不是滚动条的出现。

最佳答案

对于 future 读者,bug is still present in JDK 1.7.0_04. ,

比较 Java7 和稳定的 Java6,

enter image description here <------ Java7 对比Java6 ---> enter image description here

enter image description here <------ Java7 对比Java6 ---> enter image description here

enter image description here <------ Java7 对比Java6 ---> enter image description here

enter image description here <------ Java7 对比Java6 ---> enter image description here

来自代码

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

public class BugWrapJava7 {

    private JFrame frame = new JFrame();
    private JTextPane jtp;
    private StyledDocument doc;

    public BugWrapJava7() {
        jtp = new JTextPane();
        jtp.setText("\ntype some text in the above empty line and check the wrapping behavior");
        doc = jtp.getStyledDocument();
        doc.addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                insert();
            }

            public void removeUpdate(DocumentEvent e) {
                insert();
            }

            public void changedUpdate(DocumentEvent e) {
                insert();
            }

            public void insert() {
                SwingUtilities.invokeLater(new Runnable() {

                    public void run() {
                        Style defaultStyle = jtp.getStyle(StyleContext.DEFAULT_STYLE);
                        doc.setCharacterAttributes(0, doc.getLength(), defaultStyle, false);
                    }
                });
            }
        });
        JScrollPane scroll = new JScrollPane(jtp);
        scroll.setPreferredSize(new Dimension(200, 200));
        frame.add(scroll);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                BugWrapJava7 bugWrapJava7 = new BugWrapJava7();
            }
        });
    }
}

关于java - 带有 Java 7 的 JTextPane 中带有样式文本的奇怪文本换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11000220/

相关文章:

java - 如何在 OpenShift 上启动与 JDBC 的连接?

java - 更改 Stream 的 map 函数中字段的值

java - 资源文件中的无关文本

java - 借鉴Java重量级swing组件

java - UIManager.put ("Panel.opaque", false);不起作用

html - 断行长句的方法

delphi - 我如何知道标签字是否包含文本?

java - 在 OC4J 上配置 JAAS

java - 为什么我收到错误 : Incompatible types; JButton cannot be converted to a JTextfield

html - 如何在 Internet Explorer 的预标记中包装文本?