http://www.cjsdn.net/post/view?bid=1&id=82839&tpg=1&ppg=1&sty=1&age=0#82839
浏览网站,看到了4年前的帖子,感慨.....
写了个转换编码方法
public void jButton2_actionPerformed(ActionEvent e)
{
DefaultStyledDocument doc_old = (DefaultStyledDocument)this.jTextPane1.getDocument();
DefaultStyledDocument doc = new DefaultStyledDocument();
Element[] es = doc_old.getRootElements();
for (int i = 0; i < es.length; i++)
{
Element root = es[i];
if (root.getName().equals(DefaultStyledDocument.SectionElementName))
setElement(doc, doc_old, root);
}
this.jTextPane1.setDocument(doc);
}
private void setElement(DefaultStyledDocument doc, DefaultStyledDocument doc_old, Element e)
{
try
{
if (e.isLeaf())
{
String text = doc_old.getText(e.getStartOffset(),e.getEndOffset()-e.getStartOffset());
String tempText = new String(text.getBytes("ISO8859-1"), "GBK");
int len = doc.getLength();
doc.insertString(len, tempText, e.getAttributes());
}
else
{
int count = e.getElementCount();
for (int i = 0; i < count; i++)
{
Element ex = e.getElement(i);
setElement(doc, doc_old, ex);
}
}
}
catch (BadLocationException ex1)
{
ex1.printStackTrace();
}
catch (UnsupportedEncodingException ex)
{
/** @todo Handle this exception */
}
}