我正在制作一个科学计算器,您可以在其中编写一个完整的表达式,然后它会计算您的答案。为了显示表达式,我将使用 JEditorPane。
主要问题是:
- 显示手写的分数(分子超过分母)
- 显示权力
我为上述两个问题找到的解决方案是:
- 使用这个editorKit 来显示分数。
- 使用 HTML 来显示权力。
现在,我面临的问题是我必须使用两个 editorKit,即 HTMLEditorKit 和 FractionEditorKit,当我从 FractionEditorKit 切换到 HTMLEditorKit 时,我的所有格式都丢失了。
editorPane.setEditorKit(new FractionEditorKit());
StyledDocument doc=(StyledDocument) editorPane.getDocument();
SimpleAttributeSet attrs=new SimpleAttributeSet();
attrs.addAttribute(FractionEditorKit.FRACTION_ATTRIBUTE_NAME,"");
SimpleAttributeSet normalAttrs=new SimpleAttributeSet();
try {
doc.insertString(doc.getLength(), "Fraction ", normalAttrs);
doc.insertString(doc.getLength(), String.format("1234\r5678"), attrs);
}
catch (BadLocationException ex) {
}
}
HTMLEditorKit kitH = new HTMLEditorKit();
HTMLDocument docH=(HTMLDocument) editorPane.getDocument();
editorPane.setEditorKit(kitH);
editorPane.setDocument(docH);
try {
kitH.insertHTML(docH, editorPane.getDocument().getLength(), "<HTML>10<sup>4</sup></HTML>",0,0,null);
} catch (BadLocationException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}