Java 如何将自定义editorkit与HTMLEditorkit一起使用?

Java 如何将自定义editorkit与HTMLEditorkit一起使用?,java,swing,htmleditorkit,Java,Swing,Htmleditorkit,我正在制作一个科学计算器,你可以在其中写出一个完整的表达式,然后它会计算出你的答案。 要显示表达式,我将使用JEditorPane 主要问题是: 显示手写分数(分子比分母) 显示权力 我为上述两个问题找到的解决方案是: 使用editorKit显示分数 使用HTML显示电源 现在,我面临的问题是,我必须使用两个EditorKit,即HTMLEditorKit和FractionEditorKit,当我从FractionEditorKit切换到HTMLEditorKit时,所有格式都丢失了

我正在制作一个科学计算器,你可以在其中写出一个完整的表达式,然后它会计算出你的答案。 要显示表达式,我将使用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();
                    }
    
    editorPane.setEditorKit(新的FractionEditorKit());
    StyledDocument doc=(StyledDocument)editorPane.getDocument();
    SimpleAttributeSet attrs=新的SimpleAttributeSet();
    attrs.addAttribute(FractionEditorKit.FRACTION\u属性\u名称“”);
    SimpleAttributeSet normalAttrs=新的SimpleAttributeSet();
    试一试{
    doc.insertString(doc.getLength(),“分数”,normalAttrs);
    doc.insertString(doc.getLength(),String.format(“1234\r5678”),attrs);
    }
    捕获(BadLocationException ex){
    }
    }
    HTMLEditorKit kitH=新的HTMLEditorKit();
    HTMLDocument docH=(HTMLDocument)editorPane.getDocument();
    setEditorKit(kitH);
    editorPane.setDocument(docH);
    试一试{
    kitH.insertHTML(docH,editorPane.getDocument().getLength(),“104”,0,0,null);
    }捕获(BadLocationException e1){
    e1.printStackTrace();
    }捕获(IOE1异常){
    e1.printStackTrace();
    }
    
    Post-an。另外,请说明为什么你所做的不起作用。我已经更新了我的问题,请让tym回答。这不是一个问题。请再看一遍链接。你能告诉我我的问题在哪一方面缺乏吗?代码不完整,无法验证。请阅读链接。