模块 java.desktop

类 StyleContext

java.lang.Object
javax.swing.text.StyleContext
所有已实现的接口:
Serializable , AbstractDocument.AttributeContext
已知子类:
StyleSheet

public class StyleContext extends Object implements Serializable , AbstractDocument.AttributeContext
样式池及其相关资源。此类通过作为一个容器来确定一组资源的生命周期,该容器保存各种资源的缓存,例如被各种样式定义重用的字体和颜色。如果需要最大化相关资源的共享,这可以由多个文档共享。

此类还为小型属性集提供有效支持,并通过跨用途共享和利用它们的不可变特性来压缩它们。由于复制了许多样式,因此共享的潜力很大,而且复制品可能非常便宜。较大的集合会降低共享的可能性,因此会自动恢复为空间效率较低的实现。

Warning: 此类的序列化对象将与未来的 Swing 版本不兼容。当前的序列化支持适用于运行相同版本 Swing 的应用程序之间的短期存储或 RMI。从 1.4 开始,对所有 JavaBeans 的长期存储的支持已添加到 java.beans 包中。请参阅 XMLEncoder

  • 字段详细信息

    • DEFAULT_STYLE

      public static final String  DEFAULT_STYLE
      赋予段落的默认逻辑样式的名称。
      参见:
  • 构造方法详细信息

    • StyleContext

      public StyleContext()
      创建一个新的 StyleContext 对象。
  • 方法详情

    • getDefaultStyleContext

      public static final StyleContext  getDefaultStyleContext()
      返回所有不费心定义/提供自己的上下文的文档共享的默认 AttributeContext。
      返回:
      上下文
    • addStyle

      public Style  addStyle(String  nm, Style  parent)
      将新样式添加到样式层次结构中。样式属性从下到上解析,因此在子项中指定的属性将重写在父项中指定的属性。
      参数:
      nm - 样式的名称(在文档中命名样式的集合中必须是唯一的)。如果样式未命名,名称可能为 null,但调用者负责管理返回的引用,因为无法通过名称获取未命名样式。未命名的样式可能对诸如字符属性覆盖之类的事情很有用,例如在样式运行中发现的。
      parent - 父样式。如果不需要以某种其他样式解析未指定的属性,则这可能为 null。
      返回:
      创建的样式
    • removeStyle

      public void removeStyle(String  nm)
      删除以前添加到文档中的命名样式。
      参数:
      nm - 要删除的样式的名称
    • getStyle

      public Style  getStyle(String  nm)
      获取先前添加到文档中的命名样式
      参数:
      nm - 样式名称
      返回:
      样式
    • getStyleNames

      public Enumeration <?> getStyleNames()
      获取定义的样式的名称。
      返回:
      作为枚举的名称列表
    • addChangeListener

      public void addChangeListener(ChangeListener  l)
      添加一个监听以跟踪何时添加或删除样式。
      参数:
      l - 变化监听器
    • removeChangeListener

      public void removeChangeListener(ChangeListener  l)
      删除正在添加或删除跟踪样式的监听。
      参数:
      l - 变化监听器
    • getChangeListeners

      public ChangeListener [] getChangeListeners()
      返回使用 addChangeListener() 添加到此 StyleContext 的所有 ChangeListener 的数组。
      返回:
      添加所有 ChangeListener 或如果未添加任何监听器,则为空数组
      自从:
      1.4
    • getFont

      public Font  getFont(AttributeSet  attr)
      从属性集中获取字体。这是为了尝试为给定的 AttributeSet 获取缓存字体而实现的,如果失败,则解析字体功能并从低级字体缓存中获取字体。
      参数:
      attr - 属性集
      返回:
      字体
    • getForeground

      public Color  getForeground(AttributeSet  attr)
      获取一组属性并将其转换为前景颜色规范。这可能用于指定更亮、更多色调等内容。默认情况下,它只返回 StyleConstants.Foreground 属性指定的值。
      参数:
      attr - 属性集
      返回:
      颜色
    • getBackground

      public Color  getBackground(AttributeSet  attr)
      获取一组属性并将其转换为背景颜色规范。这可能用于指定更亮、更多色调等内容。默认情况下,它只返回 StyleConstants.Background 属性指定的值。
      参数:
      attr - 属性集
      返回:
      颜色
    • getFont

      public Font  getFont(String  family, int style, int size)
      获取新字体。如果缓存的字体存在,这会从缓存中返回一个 Font。如果不是,则将字体添加到缓存中。这基本上是 1.1 字体功能的低级缓存。
      参数:
      family - 字体系列(例如“Monospaced”)
      style - 字体的样式(例如 Font.PLAIN)
      size - 点大小 >= 1
      返回:
      新字体
    • getFontMetrics

      public FontMetrics  getFontMetrics(Font  f)
      返回字体的字体指标。
      参数:
      f - 字体
      返回:
      指标
    • addAttribute

      public AttributeSet  addAttribute(AttributeSet  old, Object  name, Object  value)
      向给定集合添加一个属性,并返回新的代表集合。

      此方法是线程安全的,尽管大多数 Swing 方法不是。请参阅 Swing 中的并发 了解更多信息。

      指定者:
      addAttribute 在接口 AbstractDocument.AttributeContext
      参数:
      old - 旧属性集
      name - 非空属性名称
      value - 属性值
      返回:
      更新后的属性集
      参见:
    • addAttributes

      public AttributeSet  addAttributes(AttributeSet  old, AttributeSet  attr)
      向元素添加一组属性。

      此方法是线程安全的,尽管大多数 Swing 方法不是。请参阅 Swing 中的并发 了解更多信息。

      指定者:
      addAttributes 在接口 AbstractDocument.AttributeContext
      参数:
      old - 旧属性集
      attr - 要添加的属性
      返回:
      更新后的属性集
      参见:
    • removeAttribute

      public AttributeSet  removeAttribute(AttributeSet  old, Object  name)
      从集合中移除一个属性。

      此方法是线程安全的,尽管大多数 Swing 方法不是。请参阅 Swing 中的并发 了解更多信息。

      指定者:
      removeAttribute 在接口 AbstractDocument.AttributeContext
      参数:
      old - 旧的属性集
      name - 非空属性名称
      返回:
      更新后的属性集
      参见:
    • removeAttributes

      public AttributeSet  removeAttributes(AttributeSet  old, Enumeration <?> names)
      删除元素的一组属性。

      此方法是线程安全的,尽管大多数 Swing 方法不是。请参阅 Swing 中的并发 了解更多信息。

      指定者:
      removeAttributes 在接口 AbstractDocument.AttributeContext
      参数:
      old - 旧属性集
      names - 属性名称
      返回:
      更新后的属性集
      参见:
    • removeAttributes

      public AttributeSet  removeAttributes(AttributeSet  old, AttributeSet  attrs)
      删除元素的一组属性。

      此方法是线程安全的,尽管大多数 Swing 方法不是。请参阅 Swing 中的并发 了解更多信息。

      指定者:
      removeAttributes 在接口 AbstractDocument.AttributeContext
      参数:
      old - 旧属性集
      attrs - 属性
      返回:
      更新后的属性集
      参见:
    • getEmptySet

      public AttributeSet  getEmptySet()
      获取一个空的 AttributeSet。
      指定者:
      getEmptySet 在接口 AbstractDocument.AttributeContext
      返回:
      集合
    • reclaim

      public void reclaim(AttributeSet  a)
      返回 MutableAttributeSet 实现不再需要的集合。这对于 1.1 下没有弱引用的操作很有用。这通常由 MutableAttributeSet 实现的 finalize 方法调用。

      此方法是线程安全的,尽管大多数 Swing 方法不是。请参阅 Swing 中的并发 了解更多信息。

      指定者:
      reclaim 在接口 AbstractDocument.AttributeContext
      参数:
      a - 要回收的集合
    • getCompressionThreshold

      protected int getCompressionThreshold()
      返回键/值对的最大数量以尝试压缩成唯一/不可变的集合。任何超过此限制的集合都将使用哈希表并成为 MutableAttributeSet。
      返回:
      门槛
    • createSmallAttributeSet

      protected StyleContext.SmallAttributeSet  createSmallAttributeSet(AttributeSet  a)
      创建一组可以共享的紧凑属性。这是想要改变 SmallAttributeSet 行为的子类的钩子。这可以重新实现以返回提供某种属性转换的 AttributeSet。
      参数:
      a - 以紧凑形式表示的属性集。
      返回:
      可能共享的一组紧凑的属性
    • createLargeAttributeSet

      protected MutableAttributeSet  createLargeAttributeSet(AttributeSet  a)
      创建大量应该以空间换取时间的属性。此集不会共享。这是一个挂钩,用于希望更改较大属性存储格式(默认情况下为 SimpleAttributeSet)的行为的子类。这可以重新实现以返回提供某种属性转换的 MutableAttributeSet。
      参数:
      a - 以较大形式表示的属性集。
      返回:
      应该以空间换取时间的大量属性
    • toString

      public String  toString()
      将 StyleContext 转换为字符串。
      重写:
      toString 在类 Object
      返回:
      字符串
    • writeAttributes

      public void writeAttributes(ObjectOutputStream  out, AttributeSet  a) throws IOException
      写出属性的上下文特定处理
      参数:
      out - 输出流
      a - 属性集
      抛出:
      IOException - 出现任何 I/O 错误
    • readAttributes

      public void readAttributes(ObjectInputStream  in, MutableAttributeSet  a) throws ClassNotFoundException , IOException
      读取属性的上下文特定处理
      参数:
      in - 从中读取属性数据的对象流。
      a - 放置属性定义的属性集。
      抛出:
      ClassNotFoundException - 如果在读取对象流时遇到,则向上传递。
      IOException - 如果在读取对象流时遇到,则向上传递。
    • writeAttributeSet

      public static void writeAttributeSet(ObjectOutputStream  out, AttributeSet  a) throws IOException
      将一组属性写入给定的对象流以进行序列化。这将特别注意处理已使用 registerStaticAttributeKey 方法注册的静态属性键。任何未注册为静态键的属性键将被直接序列化。所有值都应该是可序列化的。
      参数:
      out - 输出流
      a - 属性集
      抛出:
      IOException - 出现任何 I/O 错误
    • readAttributeSet

      public static void readAttributeSet(ObjectInputStream  in, MutableAttributeSet  a) throws ClassNotFoundException , IOException
      从给定的对象输入流中读取一组属性,这些属性先前已用 writeAttributeSet 写出。这将尝试将静态对象的密钥恢复为当前虚拟机中的静态对象,只考虑那些已使用 registerStaticAttributeKey 方法注册的密钥。从流中检索到的属性将被放入给定的可变集中。
      参数:
      in - 从中读取属性数据的对象流。
      a - 放置属性定义的属性集。
      抛出:
      ClassNotFoundException - 如果在读取对象流时遇到,则向上传递。
      IOException - 如果在读取对象流时遇到,则向上传递。
    • registerStaticAttributeKey

      public static void registerStaticAttributeKey(Object  key)
      将对象注册为静态对象,用作属性集中的键。这允许对密钥进行特殊处理以进行序列化。

      对于 1.1 虚拟机下的操作,这使用 toString 返回的值连接到类名。 toString 返回的值不应包含类引用(即它应根据 Object 中的定义重新实现),以便稍后重新计算时相同。

      参数:
      key - 非空对象键
    • getStaticAttribute

      public static Object  getStaticAttribute(Object  key)
      返回先前在 registerStaticAttributeKey 注册的对象。
      参数:
      key - 对象键
      返回:
      返回之前用 registerStaticAttributeKey 注册的对象
    • getStaticAttributeKey

      public static Object  getStaticAttributeKey(Object  key)
      返回 key 将注册的字符串。
      参数:
      key - 对象键
      返回:
      key 将注册的字符串
      参见: