JavaTM 2 Platform
Standard Ed. 6

javax.swing.text
类 StyleContext

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

public class StyleContext
extends Object
implements Serializable, AbstractDocument.AttributeContext

样式池及其关联的资源。此类用于确定资源组的生存期,假定该资源组是一个容器,它保持各种资源的缓存,这些资源(如字体和颜色等)通过各种样式定义得以重用。如果需要最大化相关资源的共享,则可由多个文档来共享此类。

此类还对小属性集提供有效支持,并通过共享和利用其不变的特性来压缩这些小属性集。因为许多样式都是重复的,所以共享的潜能非常大,而且复制成本也相当低廉。较大的集合降低了共享的可能性,并因此自动转换回一种空间利用率低的实现。

警告:此类的已序列化对象与以后的 Swing 版本不兼容。当前序列化支持适用于短期存储或运行相同 Swing 版本的应用程序之间的 RMI。从 1.4 版本开始,已在 java.beans 包中添加了支持所有 JavaBeansTM 长期存储的功能。请参见 XMLEncoder


嵌套类摘要
 class StyleContext.NamedStyle
          通常用于表示字符和段落样式的属性集合。
 class StyleContext.SmallAttributeSet
          此类可在数组中存储少量的属性。
 
字段摘要
static String DEFAULT_STYLE
          赋予与段落相关的默认逻辑样式的名称。
 
构造方法摘要
StyleContext()
          创建新的 StyleContext 对象。
 
方法摘要
 AttributeSet addAttribute(AttributeSet old, Object name, Object value)
          向给定的集合中添加属性,并返回新的有代表性的集合。
 AttributeSet addAttributes(AttributeSet old, AttributeSet attr)
          向元素添加属性集。
 void addChangeListener(ChangeListener l)
          添加一个侦听器,以跟踪何时添加或删除样式。
 Style addStyle(String nm, Style parent)
          将新的样式添加到样式层次中。
protected  MutableAttributeSet createLargeAttributeSet(AttributeSet a)
          创建一个以空间来换取时间的大属性集合。
protected  StyleContext.SmallAttributeSet createSmallAttributeSet(AttributeSet a)
          创建一个可能用于共享的压缩属性集。
 Color getBackground(AttributeSet attr)
          使用一个属性集并将其转换成为一个背景色规范。
 ChangeListener[] getChangeListeners()
          返回使用 addChangeListener() 添加到此 StyleContext 中的所有 ChangeListener 组成的数组。
protected  int getCompressionThreshold()
          返回键/值对的最大数目,尝试将其压缩到一个惟一/不可变的集合中。
static StyleContext getDefaultStyleContext()
          返回由所有文档共享的默认 AttributeContext,这些文档不影响定义/提供其自己的上下文。
 AttributeSet getEmptySet()
          获取空 AttributeSet。
 Font getFont(AttributeSet attr)
          从属性集中获取字体。
 Font getFont(String family, int style, int size)
          获取新字体。
 FontMetrics getFontMetrics(Font f)
          获取某种字体的字体规格。
 Color getForeground(AttributeSet attr)
          使用一个属性集并将其转换成为一个前景色规范。
static Object getStaticAttribute(Object key)
          返回以前用 registerStaticAttributeKey 注册的对象。
static Object getStaticAttributeKey(Object key)
          返回其 key 将要注册的 String
 Style getStyle(String nm)
          获取以前添加到文档中的已命名样式
 Enumeration<?> getStyleNames()
          获取定义的样式名称。
 void readAttributes(ObjectInputStream in, MutableAttributeSet a)
          读入属性的特定于上下文的处理。
static void readAttributeSet(ObjectInputStream in, MutableAttributeSet a)
          从以前通过 writeAttributeSet 写出的给定对象输入流中读取一个属性集。
 void reclaim(AttributeSet a)
          返回 MutableAttributeSet 实现不再需要的集合。
static void registerStaticAttributeKey(Object key)
          将一个对象注册为用作属性集中的一个键的静态对象。
 AttributeSet removeAttribute(AttributeSet old, Object name)
          从集合中移除一个属性。
 AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs)
          移除元素的属性集。
 AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names)
          移除元素的一个属性集。
 void removeChangeListener(ChangeListener l)
          移除一个跟踪正被添加或移除样式的侦听器。
 void removeStyle(String nm)
          移除以前添加到文档中的已命名样式。
 String toString()
          将 StyleContext 转换成为 String。
 void writeAttributes(ObjectOutputStream out, AttributeSet a)
          写出属性的特定于上下文的处理。
static void writeAttributeSet(ObjectOutputStream out, AttributeSet a)
          将属性集写入到给定的对象流以便序列化。
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

字段详细信息

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 方法不是线程安全的,但此方法是线程安全的。有关更多信息,请参阅 How to Use Threads

指定者:
接口 AbstractDocument.AttributeContext 中的 addAttribute
参数:
old - 原有属性集
name - 非 null 属性名称
value - 属性值
返回:
更新后的属性集
另请参见:
MutableAttributeSet.addAttribute(java.lang.Object, java.lang.Object)

addAttributes

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

虽然大多数 Swing 方法不是线程安全的,但此方法是线程安全的。有关更多信息,请参阅 How to Use Threads

指定者:
接口 AbstractDocument.AttributeContext 中的 addAttributes
参数:
old - 原有属性集
attr - 要添加的属性
返回:
更新后的属性集
另请参见:
MutableAttributeSet.addAttribute(java.lang.Object, java.lang.Object)

removeAttribute

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

虽然大多数 Swing 方法不是线程安全的,但此方法是线程安全的。有关更多信息,请参阅 How to Use Threads

指定者:
接口 AbstractDocument.AttributeContext 中的 removeAttribute
参数:
old - 原有的属性集
name - 非 null 属性名称
返回:
更新后的属性集
另请参见:
MutableAttributeSet.removeAttribute(java.lang.Object)

removeAttributes

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

虽然大多数 Swing 方法不是线程安全的,但此方法是线程安全的。有关更多信息,请参阅 How to Use Threads

指定者:
接口 AbstractDocument.AttributeContext 中的 removeAttributes
参数:
old - 原有属性集
names - 属性名
返回:
更新后的属性集
另请参见:
MutableAttributeSet.removeAttributes(java.util.Enumeration)

removeAttributes

public AttributeSet removeAttributes(AttributeSet old,
                                     AttributeSet attrs)
移除元素的属性集。

虽然大多数 Swing 方法不是线程安全的,但此方法是线程安全的。有关更多信息,请参阅 How to Use Threads

指定者:
接口 AbstractDocument.AttributeContext 中的 removeAttributes
参数:
old - 原有属性集
attrs - 属性
返回:
更新后的属性集
另请参见:
MutableAttributeSet.removeAttributes(java.util.Enumeration)

getEmptySet

public AttributeSet getEmptySet()
获取空 AttributeSet。

指定者:
接口 AbstractDocument.AttributeContext 中的 getEmptySet
返回:
集合

reclaim

public void reclaim(AttributeSet a)
返回 MutableAttributeSet 实现不再需要的集合。因为在 1.1 以下没有弱引用,所以这对于操作很有用。此方法通常由 MutableAttributeSet 实现的初始化方法所调用。

虽然大多数 Swing 方法不是线程安全的,但此方法是线程安全的。有关更多信息,请参阅 How to Use Threads

指定者:
接口 AbstractDocument.AttributeContext 中的 reclaim
参数:
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 转换成为 String。

覆盖:
Object 中的 toString
返回:
字符串

writeAttributes

public void writeAttributes(ObjectOutputStream out,
                            AttributeSet a)
                     throws IOException
写出属性的特定于上下文的处理。

抛出:
IOException

readAttributes

public void readAttributes(ObjectInputStream in,
                           MutableAttributeSet a)
                    throws ClassNotFoundException,
                           IOException
读入属性的特定于上下文的处理。

抛出:
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 - 非 null 对象键

getStaticAttribute

public static Object getStaticAttribute(Object key)
返回以前用 registerStaticAttributeKey 注册的对象。


getStaticAttributeKey

public static Object getStaticAttributeKey(Object key)
返回其 key 将要注册的 String

另请参见:
getStaticAttribute(java.lang.Object), registerStaticAttributeKey(java.lang.Object)

JavaTM 2 Platform
Standard Ed. 6

提交错误或意见

版权所有 2008 Sun Microsystems, Inc. 保留所有权利。请遵守GNU General Public License, version 2 only