- java.lang.Object
-
- javax.swing.text.StyleContext
-
- All Implemented Interfaces:
-
Serializable,AbstractDocument.AttributeContext
- 已知直接子类:
-
StyleSheet
public class StyleContext extends Object implements Serializable, AbstractDocument.AttributeContext
一组风格及其相关资源。 该类通过作为容纳各种样式定义重用的各种资源(如字体和颜色)的缓存的容器来确定一组资源的生命周期。 如果需要,可以由多个文档共享,以最大限度地分享相关资源。这个类还提供了对小组属性的有效支持,并通过共享使用并利用其不变性来压缩它们。 由于许多风格被复制,共享的潜力是重要的,副本可以非常便宜。 较大的集减少了共享的可能性,因此自动恢复到更少的空间有效的实现。
警告:此类的序列化对象与将来的Swing版本不兼容。 当前的序列化支持适用于运行相同版本的Swing的应用程序之间的短期存储或RMI。 从1.4开始,支持所有JavaBeans的长期存储已被添加到
java.beans软件包中。 请参阅XMLEncoder。- 另请参见:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class 描述 classStyleContext.NamedStyle属性集合,通常用于表示字符和段落样式。classStyleContext.SmallAttributeSet该类在数组中保存少量属性。
-
Field Summary
Fields Modifier and Type Field 描述 static StringDEFAULT_STYLE名称附加到段落的默认逻辑风格。
-
构造方法摘要
构造方法 Constructor 描述 StyleContext()创建一个新的StyleContext对象。
-
方法摘要
所有方法 静态方法 接口方法 具体的方法 Modifier and Type 方法 描述 AttributeSetaddAttribute(AttributeSet old, Object name, Object value)向给定集添加属性,并返回新的代表集。AttributeSetaddAttributes(AttributeSet old, AttributeSet attr)向元素添加一组属性。voidaddChangeListener(ChangeListener l)添加一个侦听器来跟踪何时添加或删除样式。StyleaddStyle(String nm, Style parent)在样式层次结构中添加新样式。protected MutableAttributeSetcreateLargeAttributeSet(AttributeSet a)创建一个大的属性集,应该与时间交换空间。protected StyleContext.SmallAttributeSetcreateSmallAttributeSet(AttributeSet a)创建可以共享的一组紧凑的属性。ColorgetBackground(AttributeSet attr)获取一组属性并将其转换为背景颜色规范。ChangeListener[]getChangeListeners()返回使用addChangeListener()添加到此StyleContext的所有ChangeListener的数组。protected intgetCompressionThreshold()返回尝试压缩到唯一/不可变集合的键/值对的最大数量。static StyleContextgetDefaultStyleContext()返回默认的AttributeContext由所有不想定义/提供自己的上下文的文档共享。AttributeSetgetEmptySet()获取一个空的AttributeSet。FontgetFont(String family, int style, int size)获取一个新的字体。FontgetFont(AttributeSet attr)从属性集获取字体。FontMetricsgetFontMetrics(Font f)返回字体的字体指标。ColorgetForeground(AttributeSet attr)获取一组属性,并将其转换为前景颜色规范。static ObjectgetStaticAttribute(Object key)返回先前使用registerStaticAttributeKey注册的对象。static ObjectgetStaticAttributeKey(Object key)返回key注册的字符串。StylegetStyle(String nm)获取先前添加到文档的命名样式Enumeration<?>getStyleNames()获取定义的样式的名称。voidreadAttributes(ObjectInputStream in, MutableAttributeSet a)属性中阅读的上下文特定处理static voidreadAttributeSet(ObjectInputStream in, MutableAttributeSet a)从给定的对象输入流读取一组属性,这些属性先前已经用writeAttributeSet。voidreclaim(AttributeSet a)返回MutableAttributeSet实现不再需要的集合。static voidregisterStaticAttributeKey(Object key)将对象注册为作为属性集中的键的静态对象。AttributeSetremoveAttribute(AttributeSet old, Object name)从集合中删除属性。AttributeSetremoveAttributes(AttributeSet old, Enumeration<?> names)删除元素的一组属性。AttributeSetremoveAttributes(AttributeSet old, AttributeSet attrs)删除元素的一组属性。voidremoveChangeListener(ChangeListener l)删除正在添加或删除的跟踪样式的侦听器。voidremoveStyle(String nm)删除之前添加到文档的命名样式。StringtoString()将StyleContext转换为字符串。voidwriteAttributes(ObjectOutputStream out, AttributeSet a)编写属性的上下文特定处理static voidwriteAttributeSet(ObjectOutputStream out, AttributeSet a)将一组属性写入给定的对象流以进行序列化。
-
-
-
字段详细信息
-
DEFAULT_STYLE
public static final String DEFAULT_STYLE
名称附加到段落的默认逻辑风格。- 另请参见:
- Constant Field Values
-
-
方法详细信息
-
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- 要删除的样式的名称
-
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)
获取一个新的字体。 如果存在高速缓存的字体,则从缓存返回一个字体。 如果没有,则会将一个字体添加到缓存中。 这基本上是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方法都不是。 详情请见Concurrency in Swing 。
- Specified by:
-
addAttribute在接口AbstractDocument.AttributeContext - 参数
-
old- 旧属性集 -
name- 非空属性名称 -
value- 属性值 - 结果
- 更新的属性集
- 另请参见:
-
MutableAttributeSet.addAttribute(java.lang.Object, java.lang.Object)
-
addAttributes
public AttributeSet addAttributes(AttributeSet old, AttributeSet attr)
向元素添加一组属性。这种方法是线程安全的,尽管大多数Swing方法都不是。 详情请参阅Concurrency in Swing 。
- Specified by:
-
addAttributes接口AbstractDocument.AttributeContext - 参数
-
old- 旧的属性集 -
attr- 要添加的属性 - 结果
- 更新的属性集
- 另请参见:
-
MutableAttributeSet.addAttribute(java.lang.Object, java.lang.Object)
-
removeAttribute
public AttributeSet removeAttribute(AttributeSet old, Object name)
从集合中删除属性。这种方法是线程安全的,尽管大多数Swing方法都不是。 详情请见Concurrency in Swing 。
- Specified by:
-
removeAttribute接口AbstractDocument.AttributeContext - 参数
-
old- 旧的属性集 -
name- 非空属性名称 - 结果
- 更新的属性集
- 另请参见:
-
MutableAttributeSet.removeAttribute(java.lang.Object)
-
removeAttributes
public AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names)
删除元素的一组属性。这种方法是线程安全的,尽管大多数Swing方法都不是。 详情请见Concurrency in Swing 。
- Specified by:
-
removeAttributes接口AbstractDocument.AttributeContext - 参数
-
old- 旧的属性集 -
names- 属性名称 - 结果
- 更新的属性集
- 另请参见:
-
MutableAttributeSet.removeAttributes(java.util.Enumeration<?>)
-
removeAttributes
public AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs)
删除元素的一组属性。这种方法是线程安全的,尽管大多数Swing方法都不是。 详情请见Concurrency in Swing 。
- Specified by:
-
removeAttributes接口AbstractDocument.AttributeContext - 参数
-
old- 旧属性集 -
attrs- 属性 - 结果
- 更新的属性集
- 另请参见:
-
MutableAttributeSet.removeAttributes(java.util.Enumeration<?>)
-
getEmptySet
public AttributeSet getEmptySet()
获取一个空的AttributeSet。- Specified by:
-
getEmptySet在接口AbstractDocument.AttributeContext - 结果
- 集合
-
reclaim
public void reclaim(AttributeSet a)
返回MutableAttributeSet实现不再需要的集合。 这对于1.1中没有弱参考的操作很有用。 这通常由MutableAttributeSet实现的finalize方法调用。这种方法是线程安全的,尽管大多数Swing方法都不是。 详情请参阅Concurrency in Swing 。
- Specified by:
-
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- 要以较大形式表示的属性集。 - 结果
- 一大堆属性应该与时间交换空间
-
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- 当读取对象流时遇到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- 读取对象流时遇到ClassNotFoundException。 -
IOException- 当读取对象流时遇到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将被注册的字符串 - 另请参见:
-
getStaticAttribute(java.lang.Object),registerStaticAttributeKey(java.lang.Object)
-
-