【问题标题】:Different JEditorPanes show html content, using the same css rules不同的 JEditorPanes 显示 html 内容,使用相同的 css 规则
【发布时间】:2016-02-20 05:16:16
【问题描述】:

一个简单的 Swing 应用程序绘制了两个独立的 JDialog,其中包含具有不同 html 内容的不同 JEditorPanes。在一个 JEditorPane 中,我们使用 css 规则来设置表格的边框可见。但是另一个 JEditorPane 使用相同的 css 规则并绘制 3px 表格边框,但它不应该(我们没有明确设置它)。为什么他们使用相同的 css 规则以及我们如何修复它?

public static void main(String args[]) {
    String text1 = "<html><body><table><tr><td>somthing ONE</td></tr></table></body></html>";
    String text2 = "<html><body><table><tr><td>somthing TWO</td></tr></table></body></html>";

    JDialog jd = new JDialog();
    JEditorPane jep = new JEditorPane();
    HTMLEditorKit kit = new HTMLEditorKit();
    HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
    jep.setEditorKit(kit);
    jep.setDocument(doc);
    setCSS(kit);
    jep.setText(text1);
    jd.getContentPane().add(jep);
    jd.pack();  
    jd.setVisible(true);

    JDialog jd2 = new JDialog();
    JEditorPane jep2 = new JEditorPane();
    HTMLEditorKit kit2 = new HTMLEditorKit();
    HTMLDocument doc2 = (HTMLDocument) kit2.createDefaultDocument();
    jep2.setEditorKit(kit2);
    jep2.setDocument(doc2);
    //We do not install css rules explicitly here
    jep2.setText(text2);
    jd2.getContentPane().add(jep2);
    jd2.pack();
    jd2.setVisible(true);
}


public static void setCSS(HTMLEditorKit kit) {
    StyleSheet styleSheet = kit.getStyleSheet();
    styleSheet.addRule("td {border-width: 3px; border-style: solid; border-color: #000000;}");
    kit.setStyleSheet(styleSheet);
}

UPD:正如 Freek de Bruijn 所说,这不是错误,并且已记录在案。因此,当我们在 HTMLEditorKit 中设置或获取 StyleSheet 时,它使用 AppContext 中的 StyleSheet,因此它在 HTMLEditorKit 的所有实例之间共享 StyleSheet,因此解决此问题的唯一方法是覆盖 HTMLEditorKit 的方法。我是这样做的:

public static class CustomKit extends HTMLEditorKit {
    private StyleSheet styles;

    @Override
    public void setStyleSheet(StyleSheet styleSheet) {
        styles = styleSheet;
    }
    @Override
    public StyleSheet getStyleSheet() {
        if (styles == null) {
            styles = super.getStyleSheet();
        }
        return styles;
    }
}

setCSS 方法现在看起来像:

public static void setCSS(CustomKit kit) {
    StyleSheet styleSheet = new StyleSheet();
    styleSheet.addRule("td {border-width: 3px; border-style: solid; border-color: #000000;}");
    kit.setStyleSheet(styleSheet);
}

【问题讨论】:

    标签: java html css swing


    【解决方案1】:

    似乎StyleSheet 对象被所有HTMLEditorKit 实例共享。来自HTMLEditorKit.getStyleSheet 方法的Javadoc 注释:

    * Get the set of styles currently being used to render the
    * HTML elements.  By default the resource specified by
    * DEFAULT_CSS gets loaded, and is shared by all HTMLEditorKit
    * instances.
    

    【讨论】:

    • 非常感谢!我刚刚将 setCSS 方法的第一行修改为“StyleSheet styleSheet = new StyleSheet();”。不是它正常工作。谢谢!
    • 很高兴解决方案如此简单!
    • 我太着急说它昨天解决了所有问题。今天,当我尝试将解决方案移至真正的应用程序时,我注意到它 - 现在它的工作方式与它应该相反,所以第一个对话框没有边框(但它应该),第二个对话框有边框(但它不应该)。无论如何,多亏了你,我找到了解决问题的方法,经过一番调查,我设法解决了它。我更新了我的帖子以显示我的解决方案。
    • 很高兴听到您可以解决它,并感谢您使用您的解决方案更新问题。
    猜你喜欢
    • 1970-01-01
    • 2012-07-22
    • 2022-01-26
    • 2014-02-08
    • 2010-10-30
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多