- java.lang.Object
-
- javax.swing.text.StyleContext
-
- javax.swing.text.html.StyleSheet
-
- All Implemented Interfaces:
-
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规范。为了让Views存储较少的状态,因此更轻量化,StyleSheet可以作为处理一些渲染任务的画家的工厂。 这允许实现确定他们想要缓存的内容,并将共享潜在地置于多个视图的选择器共同的级别。 由于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样式应该覆盖由元素定义的视觉属性时的语义没有很好的定义。 例如,html
<body bgcolor=red>使身体有一个红色的背景。 但是,如果html文件也包含CSS规则body { background: blue },那么身体的背景应该是什么颜色就变得不太清楚了。 当前的实现给出了在元素中定义的最高优先级的视觉属性,即它们始终在任何样式之前被检查。 因此,在前面的示例中,背景将具有红色,因为主体元素将背景颜色定义为红色。如前所述,它支持CSS。 我们不支持完整的CSS规范。 请参考CSS类的javadoc来查看我们支持的属性。 我们目前不支持的两个主要的CSS解析相关概念是伪选择器,如
A:link { color: red }和important修饰符。- Implementation Note:
- 这个实现目前是不完整的。 它可以替换为完整的替代实现。 此类的未来版本将提供更好的CSS支持。
- 另请参见:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class 描述 static classStyleSheet.BoxPainter课程执行CSS格式的一些职责。static classStyleSheet.ListPainter课程执行CSS列表格式的一些职责。-
Nested classes/interfaces inherited from class javax.swing.text.StyleContext
StyleContext.NamedStyle, StyleContext.SmallAttributeSet
-
-
Field Summary
-
Fields inherited from class javax.swing.text.StyleContext
DEFAULT_STYLE
-
-
构造方法摘要
构造方法 Constructor 描述 StyleSheet()构建样式表
-
方法摘要
所有方法 静态方法 接口方法 具体的方法 Modifier and Type 方法 描述 AttributeSetaddAttribute(AttributeSet old, Object key, Object value)向给定集添加属性,并返回新的代表集。AttributeSetaddAttributes(AttributeSet old, AttributeSet attr)向元素添加一组属性。voidaddCSSAttribute(MutableAttributeSet attr, CSS.Attribute key, String value)将CSS属性添加到给定的集合。booleanaddCSSAttributeFromHTML(MutableAttributeSet attr, CSS.Attribute key, String value)将CSS属性添加到给定的集合。voidaddRule(String rule)在工作表中添加一组规则。voidaddStyleSheet(StyleSheet ss)将规范从StyleSheetss添加到接收器的规则。protected MutableAttributeSetcreateLargeAttributeSet(AttributeSet a)创建一个大的属性集,应该与时间交换空间。protected StyleContext.SmallAttributeSetcreateSmallAttributeSet(AttributeSet a)创建可以共享的一组紧凑的属性。ColorgetBackground(AttributeSet a)获取一组属性并将其转换为背景颜色规范。URLgetBase()返回基数。StyleSheet.BoxPaintergetBoxPainter(AttributeSet a)获取框格式化程序用于给定的一组CSS属性。AttributeSetgetDeclaration(String decl)将CSS声明转换为表示CSS声明的AttributeSet。FontgetFont(AttributeSet a)获取用于给定属性集的字体。ColorgetForeground(AttributeSet a)获取一组属性,并将其转换为前景颜色规范。static intgetIndexOfSize(float pt)返回HTML / CSS大小模型的索引。StyleSheet.ListPaintergetListPainter(AttributeSet a)获取列表格式化程序以用于给定的一组CSS属性。floatgetPointSize(int index)给定大小索引返回点大小。floatgetPointSize(String size)给定一个字符串,如“+2”,“-2”或“2”,返回一个点大小值。StylegetRule(String selector)获取与字符串形式给出的选择器最匹配的规则。StylegetRule(HTML.Tag t, Element e)获取用于呈现给定类型的HTML标签的样式。StyleSheet[]getStyleSheets()返回链接的样式表的数组。AttributeSetgetViewAttributes(View v)获取要在视图中显示的一组属性。voidimportStyleSheet(URL url)从url导入样式表。voidloadRules(Reader in, URL ref)加载一组以CSS1语法指定的规则。AttributeSetremoveAttribute(AttributeSet old, Object key)从集合中删除属性。AttributeSetremoveAttributes(AttributeSet old, Enumeration<?> names)删除元素的一组属性。AttributeSetremoveAttributes(AttributeSet old, AttributeSet attrs)删除一组属性。voidremoveStyle(String nm)删除之前添加到文档的命名样式。voidremoveStyleSheet(StyleSheet ss)从接收器中删除StyleSheetss。voidsetBase(URL base)设置基数。voidsetBaseFontSize(int sz)设置基本字体大小,有效值介于1和7之间。voidsetBaseFontSize(String size)从String中传递的基本字体大小设置。ColorstringToColor(String string)将颜色串(例如“RED”或“#NNNNNN”)转换为颜色。AttributeSettranslateHTMLToCSS(AttributeSet htmlAttrSet)将一组HTML属性转换为等效的CSS属性集。-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from class javax.swing.text.StyleContext
addChangeListener, addStyle, getChangeListeners, getCompressionThreshold, getDefaultStyleContext, getEmptySet, getFont, getFontMetrics, getStaticAttribute, getStaticAttributeKey, getStyle, getStyleNames, readAttributes, readAttributeSet, reclaim, registerStaticAttributeKey, removeChangeListener, toString, writeAttributes, writeAttributeSet
-
-
-
-
方法详细信息
-
getRule
public Style getRule(HTML.Tag t, Element e)
获取用于呈现给定类型的HTML标签的样式。 给定的元素表示标签,并且可以用于确定如果嵌套在元素内部属性将不同的情况的嵌套。- 参数
-
t- 转换为视觉属性的类型 -
e- 表示标签的元素; 该元素可用于确定嵌套在属性将不同的情况下,如果嵌套在其他元素内 - 结果
- 用于渲染标签的一组CSS属性
-
getRule
public Style getRule(String selector)
获取与字符串形式给出的选择器最匹配的规则。 其中selector是元素名称的空格分隔字符串。 例如,selector可能是'html body tr td''返回的样式的属性将随规则添加和删除而改变。 也就是说,如果要求使用选择器“table p”的规则,并且添加了一个新的规则,选择器为“p”,返回的样式将包含规则“p”中的新属性。
- 参数
-
selector- 元素名称的空格分隔字符串。 - 结果
- 与选择器最匹配的规则。
-
addRule
public void addRule(String rule)
在工作表中添加一组规则。 这些规则预计将处于有效的CSS格式。 通常,这将被解析为<style>标签的结果。- 参数
-
rule- 一套规则
-
getDeclaration
public AttributeSet getDeclaration(String decl)
将CSS声明转换为表示CSS声明的AttributeSet。 通常这将被认为是遇到HTML样式属性的结果。- 参数
-
decl- 一个CSS声明 - 结果
- 一组代表CSS声明的属性。
-
loadRules
public void loadRules(Reader in, URL ref) throws IOException
加载一组以CSS1语法指定的规则。 如果存在与现有规则的冲突,新指定的规则将获胜。- 参数
-
in- 从中读取CSS语法的流 -
ref- 参考网址。 该值表示流的位置,可以为空。 流中指定的所有相对URL将基于此参数。 - 异常
-
IOException- 如果发生I / O错误。
-
getViewAttributes
public AttributeSet getViewAttributes(View v)
获取要在视图中显示的一组属性。 这基本上是可以用于View.getAttributes的一组属性。- 参数
-
v- 一个视图 - 结果
- 的属性
-
removeStyle
public void removeStyle(String nm)
删除之前添加到文档的命名样式。- 重写:
-
removeStyle在StyleContext - 参数
-
nm- 要删除的样式的名称
-
addStyleSheet
public void addStyleSheet(StyleSheet ss)
将规则从StyleSheetss添加到接收器的规则。ss's规则将覆盖任何以前添加的样式表的规则。 添加的StyleSheet将不会覆盖接收样式表的规则。- 参数
-
ss- 样式表 - 从以下版本开始:
- 1.3
-
removeStyleSheet
public void removeStyleSheet(StyleSheet ss)
从接收器中删除StyleSheetss。- 参数
-
ss- 样式表 - 从以下版本开始:
- 1.3
-
getStyleSheets
public StyleSheet[] getStyleSheets()
返回链接的样式表的数组。 如果没有链接的样式表,将返回null。- 结果
- 一系列的StyleSheets。
- 从以下版本开始:
- 1.3
-
importStyleSheet
public void importStyleSheet(URL url)
从url导入样式表。 生成的规则直接添加到接收器。 如果您不希望规则成为接收者的一部分,请创建一个新的样式表,并使用addStyleSheet将其链接起来。- 参数
-
url- 一个网址 - 从以下版本开始:
- 1.3
-
setBase
public void setBase(URL base)
设置基数。 所有相对的import语句将相对于base。- 参数
-
base- 一个基地。 - 从以下版本开始:
- 1.3
-
getBase
public URL getBase()
返回基数。- 结果
- 的基地。
- 从以下版本开始:
- 1.3
-
addCSSAttribute
public void addCSSAttribute(MutableAttributeSet attr, CSS.Attribute key, String value)
将CSS属性添加到给定的集合。- 参数
-
attr- 一组属性 -
key- 一个CSS属性 -
value- 一个HTML属性值 - 从以下版本开始:
- 1.3
-
addCSSAttributeFromHTML
public boolean addCSSAttributeFromHTML(MutableAttributeSet attr, CSS.Attribute key, String value)
将CSS属性添加到给定的集合。- 参数
-
attr- 一组属性 -
key- 一个CSS属性 -
value- 一个HTML属性值 - 结果
-
true如果HTML属性value可以转换为CSS属性,否则为false。 - 从以下版本开始:
- 1.3
-
translateHTMLToCSS
public AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet)
将一组HTML属性转换为等效的CSS属性集。- 参数
-
htmlAttrSet- 包含HTML属性的AttributeSet。 - 结果
- 一组CSS属性。
-
addAttribute
public AttributeSet addAttribute(AttributeSet old, Object key, Object value)
向给定集添加属性,并返回新的代表集。 在将StyleConstant属性转换为超类行为之前,将重新实现此功能。 StyleConstants属性没有相应的CSS条目,StyleConstants属性被存储(但可能未被使用)。- Specified by:
-
addAttribute在接口AbstractDocument.AttributeContext - 重写:
-
addAttribute在StyleContext - 参数
-
old- 旧的属性集 -
key- 非空属性键 -
value- 属性值 - 结果
- 更新的属性集
- 另请参见:
-
MutableAttributeSet.addAttribute(java.lang.Object, java.lang.Object)
-
addAttributes
public AttributeSet addAttributes(AttributeSet old, AttributeSet attr)
向元素添加一组属性。 如果这些属性中的任何一个都是StyleConstants属性,那么在转发到超类行为之前,它们将被转换为CSS。- Specified by:
-
addAttributes在接口AbstractDocument.AttributeContext - 重写:
-
addAttributes在StyleContext - 参数
-
old- 旧属性集 -
attr- 要添加的属性 - 结果
- 更新的属性集
- 另请参见:
-
MutableAttributeSet.addAttribute(java.lang.Object, java.lang.Object)
-
removeAttribute
public AttributeSet removeAttribute(AttributeSet old, Object key)
从集合中删除属性。 如果属性是StyleConstants属性,则在转发到超类行为之前,请求将转换为CSS属性。- Specified by:
-
removeAttribute接口AbstractDocument.AttributeContext - 重写:
-
removeAttribute在StyleContext - 参数
-
old- 旧集属性 -
key- 非空属性名称 - 结果
- 更新的属性集
- 另请参见:
-
MutableAttributeSet.removeAttribute(java.lang.Object)
-
removeAttributes
public AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names)
删除元素的一组属性。 如果任何属性是StyleConstants属性,则在转发到超类行为之前,请求将转换为CSS属性。- Specified by:
-
removeAttributes在接口AbstractDocument.AttributeContext - 重写:
-
removeAttributes在StyleContext - 参数
-
old- 旧的属性集 -
names- 属性名称 - 结果
- 更新的属性集
- 另请参见:
-
MutableAttributeSet.removeAttributes(java.util.Enumeration<?>)
-
removeAttributes
public AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs)
删除一组属性。 如果任何属性是StyleConstants属性,则在转发到超类行为之前,请求将转换为CSS属性。- Specified by:
-
removeAttributes在接口AbstractDocument.AttributeContext - 重写:
-
removeAttributes在StyleContext - 参数
-
old- 旧的属性集 -
attrs- 属性 - 结果
- 更新的属性集
- 另请参见:
-
MutableAttributeSet.removeAttributes(java.util.Enumeration<?>)
-
createSmallAttributeSet
protected StyleContext.SmallAttributeSet createSmallAttributeSet(AttributeSet a)
创建可以共享的一组紧凑的属性。 这是一个希望改变SmallAttributeSet行为的子类的钩子。 这可以被重新实现以返回提供某种属性转换的AttributeSet。- 重写:
-
createSmallAttributeSet在StyleContext - 参数
-
a- 以紧凑形式表示的属性集。 - 结果
- 一组可以共享的精简属性
-
createLargeAttributeSet
protected MutableAttributeSet createLargeAttributeSet(AttributeSet a)
创建一个大的属性集,应该与时间交换空间。 此集不会共享。 这是一个希望更改较大属性存储格式(默认情况下为SimpleAttributeSet)的行为的子类的钩子。 这可以重新实现,以返回提供某种属性转换的MutableAttributeSet。- 重写:
-
createLargeAttributeSet在StyleContext - 参数
-
a- 要以较大形式表示的属性集。 - 结果
- 一大堆属性应该与时间交换空间
-
getFont
public Font getFont(AttributeSet a)
获取用于给定属性集的字体。- 重写:
-
getFont在StyleContext - 参数
-
a- 属性集 - 结果
- 字体
-
getForeground
public Color getForeground(AttributeSet a)
获取一组属性,并将其转换为前景颜色规范。 这可能用于指定更亮,更多色相等的东西。- 重写:
-
getForeground在StyleContext - 参数
-
a- 属性集 - 结果
- 颜色
-
getBackground
public Color getBackground(AttributeSet a)
获取一组属性并将其转换为背景颜色规范。 这可能用于指定更亮,更多色相等的东西。- 重写:
-
getBackground在StyleContext - 参数
-
a- 属性集 - 结果
- 颜色
-
getBoxPainter
public StyleSheet.BoxPainter getBoxPainter(AttributeSet a)
获取框格式化程序用于给定的一组CSS属性。- 参数
-
a- 一组CSS属性 - 结果
- 盒格式化。
-
getListPainter
public StyleSheet.ListPainter getListPainter(AttributeSet a)
获取列表格式化程序以用于给定的一组CSS属性。- 参数
-
a- 一组CSS属性 - 结果
- 列表格式化程序。
-
setBaseFontSize
public void setBaseFontSize(int sz)
设置基本字体大小,有效值介于1和7之间。- 参数
-
sz- 字体大小。
-
setBaseFontSize
public void setBaseFontSize(String size)
从String中传递的基本字体大小设置。 字符串可以标识特定字体大小,合法值介于1和7之间,或标识相对字体大小,例如+1或-2。- 参数
-
size- 字体大小。
-
getIndexOfSize
public static int getIndexOfSize(float pt)
返回HTML / CSS大小模型的索引。- 参数
-
pt- 点的大小 - 结果
- HTML / CSS大小模型的索引。
-
getPointSize
public float getPointSize(int index)
给定大小索引返回点大小。- 参数
-
index- 一个大小索引 - 结果
- 点大小值。
-
getPointSize
public float getPointSize(String size)
给定一个字符串,如“+2”,“-2”或“2”,返回一个点大小值。- 参数
-
size- 描述字体大小的CSS字符串 - 结果
- 点大小值。
-
-