JavaTM 2 Platform
Standard Ed. 6

javax.swing.text.html
类 StyleSheet

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

public class StyleSheet
extends StyleContext

支持定义要呈现的 HTML 视图的可视化特征。该 StyleSheet 用于将 HTML 模型转换为具有可视化特征的视图。此类可自定义视图的外观、通过一个模型分别呈现多个视图等等。该类可以看作 CSS 规则的存储库。CSS 属性的键是类型 CSS.Attribute 的对象。值类型由 StyleSheet 实现决定,但需要 toString 方法返回 CSS 值的字符串表示形式。

获取其属性的 HTML View 实现的主要入口点是 getViewAttributes 方法。通过创建所需的用来将属性与视图关联的策略来实现这一方法。每个 HTMLEditorKit(也即每个关联的 JEditorPane)都可以有其自己的 StyleSheet,但默认情况下,所有 HTMLEditorKit 实例都将共享一个表。HTMLDocument 实例还可以有一个 StyleSheet,它用来存储特定于文档的 CSS 规范。

为使 View 存储较少的状态并因此变得更轻量级,StyleSheet 可充当处理某些呈现任务的 painter 的工厂。此方法允许实现来确定要缓存的内容,以及在选择器通常为多视图的级别上可能共享的内容。由于 StyleSheet 可以由面向多个文档的视图使用,并且通常 HTML 属性不影响使用的选择器,所以共享选择器的可能性非常大。

这些规则是按指定样式存储的,并且还存储了其他信息以便将元素的上下文快速转换为一个规则。以下代码片段将显示指定的样式,以及由此所包含的 CSS 规则。

   
   import java.util.*;
   import javax.swing.text.*;
   import javax.swing.text.html.*;
   
   public class ShowStyles {
   
       public static void main(String[] args) {
           HTMLEditorKit kit = new HTMLEditorKit();
           HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
           StyleSheet styles = doc.getStyleSheet();
           
           Enumeration rules = styles.getStyleNames();
           while (rules.hasMoreElements()) {
               String name = (String) rules.nextElement();
               Style rule = styles.getStyle(name);
               System.out.println(rule.toString());
           }
           System.exit(0);
       }
   }
   
 

此语义用于 CSS 样式未良好定义的情况,在这种情况下,CSS 样式应根据元素的定义重写可视属性。例如,html <body bgcolor=red> 将使主体呈现为红色背景。但如果该 html 文件还包含 CSS 规则 body { background:blue },则该主体的背景就没有它该呈现的颜色清晰了。当前的实现为在元素中定义的可视属性提供了最高优先权,也就是说在应用任何样式之前都要先检查它们。因此,在前面的示例中,当主体元素将背景颜色定义为红色时,其背景也将成为红色。

正如前面提到的那样,此方法支持 CSS。我们不提供对所有 CSS 规范的支持。请参考 CSS 类的 javadoc 以查看我们支持的属性。我们当前不支持的两种主要的 CSS 解析相关的概念是伪选择器,如 A:link { color:red }important 修饰符。

注:此实现目前尚不完整。可以使用完整的替代实现来取代它。此类的未来版本将提供更好的 CSS 支持。


嵌套类摘要
static class StyleSheet.BoxPainter
          可以执行某些 CSS 格式化任务的类。
static class StyleSheet.ListPainter
          要执行某些 CSS 列表格式化任务的类。
 
从类 javax.swing.text.StyleContext 继承的嵌套类/接口
StyleContext.NamedStyle, StyleContext.SmallAttributeSet
 
字段摘要
 
从类 javax.swing.text.StyleContext 继承的字段
DEFAULT_STYLE
 
构造方法摘要
StyleSheet()
          构造 StyleSheet
 
方法摘要
 AttributeSet addAttribute(AttributeSet old, Object key, Object value)
          向给定集合添加属性,并返回新的表示方式的集合。
 AttributeSet addAttributes(AttributeSet old, AttributeSet attr)
          向元素添加属性集。
 void addCSSAttribute(MutableAttributeSet attr, CSS.Attribute key, String value)
          将 CSS 属性添加到给定的集合。
 boolean addCSSAttributeFromHTML(MutableAttributeSet attr, CSS.Attribute key, String value)
          将 CSS 属性添加到给定的集合。
 void addRule(String rule)
          向 sheet 添加规则集合。
 void addStyleSheet(StyleSheet ss)
          将 StyleSheet ss 中的规则添加到接收者的规则中。
protected  MutableAttributeSet createLargeAttributeSet(AttributeSet a)
          创建一个以空间来换取时间的大属性集合。
protected  StyleContext.SmallAttributeSet createSmallAttributeSet(AttributeSet a)
          创建可以共享的属性压缩集。
 Color getBackground(AttributeSet a)
          获取一个属性集并将其转换为背景色规范。
 URL getBase()
          返回 base。
 StyleSheet.BoxPainter getBoxPainter(AttributeSet a)
          获取给定 CSS 属性集合使用的框 formatter。
 AttributeSet getDeclaration(String decl)
          将 CSS 声明转换为表示 CSS 声明的 AttributeSet。
 Font getFont(AttributeSet a)
          获取给定属性集使用的字体。
 Color getForeground(AttributeSet a)
          获取一个属性集并将其转换为前景色规范。
static int getIndexOfSize(float pt)
           
 StyleSheet.ListPainter getListPainter(AttributeSet a)
          获取给定 CSS 属性集合使用的列表 formatter。
 float getPointSize(int index)
          在给定大小索引的情况下返回点大小。
 float getPointSize(String size)
          如果给定字符串(如 "+2"、"-2" 或 "2"),则返回点大小值。
 Style getRule(HTML.Tag t, Element e)
          获取用来呈现给定类型的 HTML 标记的样式。
 Style getRule(String selector)
          获取最能匹配以字符串形式表示的选择器的规则。
 StyleSheet[] getStyleSheets()
          返回一个链接 StyleSheet 的数组。
 AttributeSet getViewAttributes(View v)
          获取用来显示视图的属性集。
 void importStyleSheet(URL url)
          从 url 导入样式表。
 void loadRules(Reader in, URL ref)
          加载 CSS1 语法中指定的规则集合。
 AttributeSet removeAttribute(AttributeSet old, Object key)
          从集合中移除属性。
 AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs)
          移除属性集。
 AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names)
          移除元素的属性集。
 void removeStyle(String nm)
          移除以前添加到该文档中的指定样式。
 void removeStyleSheet(StyleSheet ss)
          从接收者的规则中移除 StyleSheet ss
 void setBase(URL base)
          设置 base。
 void setBaseFontSize(int sz)
          设置基本字体大小,其有效值范围为 1 到 7。
 void setBaseFontSize(String size)
          根据传入的 String 设置基本字体大小。
 Color stringToColor(String string)
          将颜色字符串(如 "RED" 或 "#NNNNNN")转换为 Color。
 AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet)
          将 HTML 属性集合转换为对等的 CSS 属性集合。
 
从类 javax.swing.text.StyleContext 继承的方法
addChangeListener, addStyle, getChangeListeners, getCompressionThreshold, getDefaultStyleContext, getEmptySet, getFont, getFontMetrics, getStaticAttribute, getStaticAttributeKey, getStyle, getStyleNames, readAttributes, readAttributeSet, reclaim, registerStaticAttributeKey, removeChangeListener, toString, writeAttributes, writeAttributeSet
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

构造方法详细信息

StyleSheet

public StyleSheet()
构造 StyleSheet

方法详细信息

getRule

public Style getRule(HTML.Tag t,
                     Element e)
获取用来呈现给定类型的 HTML 标记的样式。给定的元素代表标记,可用来确定是什么样的嵌套情形:是嵌套在元素的内部还是外部,这两种情况的属性是不一样的。

参数:
t - 将转换为可视属性的类型
e - 表示标记的元素;该元素可用于确定嵌套的情况,如果嵌套在其他元素的内部,则属性将不同
返回:
用来呈现标记的 CSS 属性集合

getRule

public Style getRule(String selector)
获取最能匹配以字符串形式表示的选择器的规则。其中 selector 是由空格隔开的元素名称 String。例如,selector 可以是 'html body tr td'

返回的 Style 属性将随着添加和移除规则进行更改。也就是说,如果要请求一个具有选择器 "table p" 的规则,并添加了具有选择器 "p" 的新规则,则返回的 Style 将包括来自规则 "p" 的新属性。


addRule

public void addRule(String rule)
向 sheet 添加规则集合。该规则应是有效的 CSS 格式。通常情况下将根据对 <style> 标记的解析结果调用此方法。


getDeclaration

public AttributeSet getDeclaration(String decl)
将 CSS 声明转换为表示 CSS 声明的 AttributeSet。通常在遇到 HTML 样式属性时将调用此方法。


loadRules

public void loadRules(Reader in,
                      URL ref)
               throws IOException
加载 CSS1 语法中指定的规则集合。如果与现有的规则发生了冲突,则保留新指定的规则。

参数:
in - 要从其读取 CSS 语法的流
ref - 引用 URL。此值表示流的位置,可以为 null。所有在流中指定的相关 URL 都将以此参数为依据。
抛出:
IOException

getViewAttributes

public AttributeSet getViewAttributes(View v)
获取用来显示视图的属性集。这基本上是一个可用于 View.getAttributes 的属性集。


removeStyle

public void removeStyle(String nm)
移除以前添加到该文档中的指定样式。

覆盖:
StyleContext 中的 removeStyle
参数:
nm - 要移除的样式的名称

addStyleSheet

public void addStyleSheet(StyleSheet ss)
将 StyleSheet ss 中的规则添加到接收者的规则中。ss's 规则将重写以前添加的所有样式表的规则。添加的 StyleSheet 永远不会重写接收样式表的规则。

从以下版本开始:
1.3

removeStyleSheet

public void removeStyleSheet(StyleSheet ss)
从接收者的规则中移除 StyleSheet ss

从以下版本开始:
1.3

getStyleSheets

public StyleSheet[] getStyleSheets()
返回一个链接 StyleSheet 的数组。如果不存在链接 StyleSheet,则返回 null。

从以下版本开始:
1.3

importStyleSheet

public void importStyleSheet(URL url)
url 导入样式表。得到的规则将直接添加到接收者中。如果不想让这些规则成为接收者的一部分,则要创建一个新的 StyleSheet,并使用 addStyleSheet 与其链接。

从以下版本开始:
1.3

setBase

public void setBase(URL base)
设置 base。所有相关的导入语句都将与 base 相关。

从以下版本开始:
1.3

getBase

public URL getBase()
返回 base。

从以下版本开始:
1.3

addCSSAttribute

public void addCSSAttribute(MutableAttributeSet attr,
                            CSS.Attribute key,
                            String value)
将 CSS 属性添加到给定的集合。

从以下版本开始:
1.3

addCSSAttributeFromHTML

public boolean addCSSAttributeFromHTML(MutableAttributeSet attr,
                                       CSS.Attribute key,
                                       String value)
将 CSS 属性添加到给定的集合。

从以下版本开始:
1.3

translateHTMLToCSS

public AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet)
将 HTML 属性集合转换为对等的 CSS 属性集合。

参数:
htmlAttrSet - 包含 HTML 属性的 AttributeSet。

addAttribute

public AttributeSet addAttribute(AttributeSet old,
                                 Object key,
                                 Object value)
向给定集合添加属性,并返回新的表示方式的集合。实现此方法以将 StyleConstant 属性转换为 CSS,然后转发给超类行为。StyleConstants 属性没有相应的 CSS 项,将会存储 StyleConstants 属性(但不一定有用)。

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

addAttributes

public AttributeSet addAttributes(AttributeSet old,
                                  AttributeSet attr)
向元素添加属性集。如果这些属性都是 StyleConstants 属性,则会将它们转换为 CSS,然后再转发给超类行为。

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

removeAttribute

public AttributeSet removeAttribute(AttributeSet old,
                                    Object key)
从集合中移除属性。如果该属性是 StyleConstants 属性,则会将请求转换为 CSS 属性,然后再转发给超类行为。

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

removeAttributes

public AttributeSet removeAttributes(AttributeSet old,
                                     Enumeration<?> names)
移除元素的属性集。如果所有属性都是 StyleConstants 属性,则该请求将会转换为 CSS 属性,然后再转发给超类行为。

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

removeAttributes

public AttributeSet removeAttributes(AttributeSet old,
                                     AttributeSet attrs)
移除属性集。如果所有属性都是 StyleConstants 属性,则该请求将会转换为 CSS 属性,然后再转发给超类行为。

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

createSmallAttributeSet

protected StyleContext.SmallAttributeSet createSmallAttributeSet(AttributeSet a)
创建可以共享的属性压缩集。这是想要改变 SmallAttributeSet 行为的子类的一个钩子。重新实现此方法以返回提供某种属性转换的 AttributeSet。

覆盖:
StyleContext 中的 createSmallAttributeSet
参数:
a - 以压缩形式表示的属性集。

createLargeAttributeSet

protected MutableAttributeSet createLargeAttributeSet(AttributeSet a)
创建一个以空间来换取时间的大属性集合。此集合将不可共享。这是想要改变更大属性存储样式(默认情况下是 SimpleAttributeSet)的子类的一个钩子。重新实现此方法以返回提供某种属性转换的 MutableAttributeSet。

覆盖:
StyleContext 中的 createLargeAttributeSet
参数:
a - 以更大形式表示的属性集。

getFont

public Font getFont(AttributeSet a)
获取给定属性集使用的字体。

覆盖:
StyleContext 中的 getFont
参数:
a - 属性集
返回:
字体

getForeground

public Color getForeground(AttributeSet a)
获取一个属性集并将其转换为前景色规范。此方法可以用于指定诸如更亮、更鲜艳等之类的内容。

覆盖:
StyleContext 中的 getForeground
参数:
a - 属性集
返回:
颜色

getBackground

public Color getBackground(AttributeSet a)
获取一个属性集并将其转换为背景色规范。此方法可以用于指定诸如更亮、更鲜艳等之类的内容。

覆盖:
StyleContext 中的 getBackground
参数:
a - 属性集
返回:
颜色

getBoxPainter

public StyleSheet.BoxPainter getBoxPainter(AttributeSet a)
获取给定 CSS 属性集合使用的框 formatter。


getListPainter

public StyleSheet.ListPainter getListPainter(AttributeSet a)
获取给定 CSS 属性集合使用的列表 formatter。


setBaseFontSize

public void setBaseFontSize(int sz)
设置基本字体大小,其有效值范围为 1 到 7。


setBaseFontSize

public void setBaseFontSize(String size)
根据传入的 String 设置基本字体大小。字符串可以标识特定字体大小(有效值在 1 和 7 之间),也可以标识相对字体大小(如 +1 或 -2)。


getIndexOfSize

public static int getIndexOfSize(float pt)

getPointSize

public float getPointSize(int index)
在给定大小索引的情况下返回点大小。


getPointSize

public float getPointSize(String size)
如果给定字符串(如 "+2"、"-2" 或 "2"),则返回点大小值。


stringToColor

public Color stringToColor(String string)
将颜色字符串(如 "RED" 或 "#NNNNNN")转换为 Color。注:此方法仅转换 HTML3.2 颜色字符串或长度为 7 的字符串;否则,将返回 null。


JavaTM 2 Platform
Standard Ed. 6

提交错误或意见

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