- java.lang.Object
-
- javax.swing.text.StyleContext
-
- 已知直接子类:
-
StyleSheet
public class StyleContext extends Object implements Serializable, AbstractDocument.AttributeContext
样式池及其相关资源。 此类通过作为容纳各种资源(如字体和颜色)的缓存来容纳一组资源的生命周期,这些资源可以被各种样式定义重用。 如果需要,这可以由多个文档共享以最大化相关资源的共享。该类还为小型属性集提供有效支持,并通过跨用途共享并利用其不可变性来压缩它们。 由于许多样式被复制,共享的可能性很大,副本可以非常便宜。 较大的集合降低了共享的可能性,因此自动恢复到空间效率较低的实现。
警告:此类的序列化对象与以后的Swing版本不兼容。 当前的序列化支持适用于运行相同版本Swing的应用程序之间的短期存储或RMI。 从1.4开始,
java.beans软件包中添加了对所有JavaBeansjava.beans长期存储的支持。 请参阅XMLEncoder。- 另请参见:
- Serialized Form
-
-
嵌套类汇总
嵌套类 变量和类型 类 描述 classStyleContext.NamedStyle一组属性,通常用于表示字符和段落样式。classStyleContext.SmallAttributeSet此类在数组中包含少量属性。
-
字段汇总
字段 变量和类型 字段 描述 static StringDEFAULT_STYLE附加到段落的默认逻辑样式的名称。
-
构造方法摘要
构造方法 构造器 描述 StyleContext()创建一个新的StyleContext对象。
-
方法摘要
所有方法 静态方法 实例方法 具体的方法 变量和类型 方法 描述 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写出的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转换为String。voidwriteAttributes(ObjectOutputStream out, AttributeSet a)写出属性的特定于上下文的处理static voidwriteAttributeSet(ObjectOutputStream out, AttributeSet a)为了序列化,将一组属性写入给定的对象流。
-
-
-
方法详细信息
-
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)
获取一个新字体。 如果存在缓存字体,则从缓存返回一个Font。 如果没有,则将字体添加到缓存中。 这基本上是1.1字体功能的低级缓存。- 参数
-
family- 字体系列(例如“Monospaced”) -
style- 字体样式(如Font.PLAIN) -
size-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- 非null属性名称 -
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- 非null属性名称 - 结果
- 更新的属性集
- 另请参见:
-
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- 在读取对象流时遇到向上传递。
-
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写出的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注册的对象。- 参数
-
key- 对象键 - 结果
-
返回先前使用
registerStaticAttributeKey注册的对象
-
getStaticAttributeKey
public static Object getStaticAttributeKey(Object key)
返回key将注册的字符串。- 参数
-
key- 对象键 - 结果
-
key将注册的字符串 - 另请参见:
-
getStaticAttribute(java.lang.Object),registerStaticAttributeKey(java.lang.Object)
-
-