我将带有 JLabel 的文本添加到 JTextPane,然后我需要更改 JTextPane 中所有 JLabel 中的文本。我该怎么做?
...
JTextPane pane = new JTextPane();
HTMLEditorKit kit = new CompEditorKit();
HTMLDocument doc = new HTMLDocument();
pane.setEditable(false);
pane.setContentType("text/html");
pane.setEditorKit(kit);
pane.setDocument(doc);
...
kit.insertHTML(doc, doc.getLength(), "Test<object align=\"left\" classid=\"javax.swing.JLabel\"><param name=\"text\" value=\"22\"></object>Test", 0, 0, null);
EditLabels(doc);
...
public void EditLabels(Document doc) {
if (doc instanceof HTMLDocument) {
Element elem = doc.getDefaultRootElement();
ElementIterator iterator = new ElementIterator(elem);
while ((elem = iterator.next()) != null) {
AttributeSet attrs = elem.getAttributes();
Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
Object o = (elementName != null) ? null : attrs.getAttribute(StyleConstants.NameAttribute);
if (o instanceof HTML.Tag) {
if ((HTML.Tag) o == HTML.Tag.OBJECT) {
View view = new CompView(elem);
//View view = (View)super.create(elem); //ERROR
//if (view instanceof JLabel)
//{
// ((JLabel) view).setText("NM");
// JLabel label = (JLabel)view;
//}
}
}
}
}
}
例如
View view = new CompView(elem);
((JLabel) view).setText("NM");
error: inconvertible types
required: JLabel
found: View
View view = (View)super.create(elem);
error: cannot find symbol
symbol: method create(Element)