我正在使用JEditorPane
创建一个编辑器, HTMLDocument
和HTMLEditorKit
。我有一个工具栏,其中包含各种组件来更改编辑器的样式属性。其中之一是 JComboBox
更改 ZOOM_FACTOR
属性。下面的代码是当 JComboBox
时执行的代码的值发生变化。
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:有一些旧报告 here在 JDK Bug 系统中。据报道出于不同的原因,但也显示了相同的 <p-implied>
正在添加到 <head></head>
然后。我知道此链接中报告的问题使用 JTextPane
( JEditorPane
的子类)和 setCharacterAttributes
JTextPane
中的方法类,但该方法也调用相同的 setCharacterAttributes
我在其内部使用的方法。
最佳答案
您使用 0 个位置,但对于 HTMLDocument,这些位置属于 HEAD(而不是 BODY)部分。
看起来您只是用它来刷新内容。您可以在文档末尾应用相同的代码。
doc.setCharacterAttributes(doc.getLength(), 1, attrs, true);
因此,属性更改事件被应用到 BODY。
关于java - 当调用 setCharacterAttributes 时,JEditorPane HTMLDocument 将 p-implied 添加到 HTML 中,这是不必要的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27657278/