2

我正在使用 和创建一个编辑JEditorPane器。我有一个工具栏,其中包含用于更改编辑器样式属性的各种组件。其中之一是更改属性。下面的代码是当值改变时执行的代码。HTMLDocumentHTMLEditorKitJComboBoxZOOM_FACTORJComboBox

    final SimpleAttributeSet attrs = new SimpleAttributeSet();
    zoomCombo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String s = (String) zoomCombo.getSelectedItem();
            s = s.substring(1, s.length());
            double scale = new Double(s).doubleValue() / 100;
            editorPane.getDocument().putProperty("ZOOM_FACTOR", new Double(scale));

            try {
                StyledDocument doc = (StyledDocument) editorPane.getDocument();
                doc.setCharacterAttributes(0, 1, attrs, true);
                doc.insertString(0, "", null); // refresh
            } catch (Exception ex) {
                logger.error("Hata", ex);
            }
        }
    });

doc.setCharacterAttributes(0, 1, attrs, true);是我的问题的根源所在的行。这行代码执行后,被<p-implied>添加<head></head>HTML text. JEditorPane.getText发生这种情况后,如果发生某种特定的事件模式,我的行为HTML text就会被破坏。有什么办法不一起创作<p-implied>吗?如果不是这样,那么解决此问题的最佳方法是什么?

PS:JDK Bug System 中有一些旧的报告。据报道,出于不同的原因,但那里也显示,后来<p-implied>添加了相同的内容<head></head>。我知道此链接中报告的问题在类中使用JTextPane(的子类JEditorPane)和setCharacterAttributes方法,JTextPane但该方法也调用了setCharacterAttributes我在其内部使用的相同方法。

4

1 回答 1

3

您使用 0 位置,但对于 HTMLDocument,位置属于 HEAD(而不是 BODY)部分。

看起来你只是用它来刷新内容。您可以在文档末尾应用相同的代码。

doc.setCharacterAttributes(doc.getLength(), 1, attrs, true);

因此,属性更改事件被应用于 BODY。

于 2014-12-26T12:47:57.313 回答