【问题标题】:How to set a global style for styled document in java swing如何在java swing中为样式化文档设置全局样式
【发布时间】:2013-10-13 14:31:49
【问题描述】:

我有一个 StyledDocument,我正在向其中添加一些样式。我想要一些适用于我创建的所有样式的默认(全局)样式。例如所有样式的全局背景,这样我就不必为每个样式指定background

这是我想要达到的目标

    public void setUpStyles() {
        parentMSGStyle = historyPane.addStyle("parentmsgstyle", null);
        userNameStyle = historyPane.addStyle("usernamestyle", parentMSGStyle);
        StyleConstants.setBackground(parentMSGStyle, Color.GRAY);
        StyleConstants.setForeground(userNameStyle, Color.BLUE);
    }

暂时没有。只有蓝色样式有效,但“灰色”样式无效。 我是 StyledDocuments 的新手。请指出正确的方向。

【问题讨论】:

    标签: java swing styles styleddocument


    【解决方案1】:

    看到这个后,我自己也尝试过,也让我感到困惑。根据childStyle = addStyle(String nm, parentStyle)JAVA DOC 应该显示以下行为:

    将新样式添加到逻辑样式层次结构中。样式属性 自下而上解析,因此子项中指定的属性将 覆盖父项中指定的属性。

    只要我们不给子元素设置背景颜色,它就不应该覆盖父样式的属性。

    StyleConstants.setBackground(parentMSGStyle, Color.GREY);
    StyleConstants.setForeground(userNameStyle, Color.BLUE);
    

    如果我们现在尝试使用document.getBackground(userNameStylee) 打印文档的背景颜色,它应该打印GRAY 并且它会这样做。 因此,由于未知原因,它不仅仅是在屏幕上进行。但令人惊讶的是,反过来也会奏效。也就是说,将Forground 设置为parentStyle,将Background 设置为childStyle

    StyleConstants.setForeground(parentMSGStyle, Color.BLUE);
    StyleConstants.setBackground(userNameStyle, Color.GREY);
    

    别告诉我这不是解决方案。 :)

    【讨论】:

      猜你喜欢
      • 2019-05-31
      • 1970-01-01
      • 2015-04-17
      • 2019-12-05
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      相关资源
      最近更新 更多