java Swing中Element的用途及其与StyledDocument和段落的关系
我试图了解 Element 的用途以及它如何帮助操作要在 JEditorPane 或 JTextPane 中显示的 StyledDocument 。我还试图理解它与“段落”概念的关系。
javax.swing.text.Element 的 javadoc 几乎什么都没有:
公共接口 Element
描述文档结构部分的接口。它旨在捕捉 SGML 元素的精神。
我非常熟悉 HTML 和 XML 中元素的概念,显然这是类似的东西,但我只是看不到它的用途,因为它与 StyledDocument 相关。起初我认为这只是 StyledDocument 在内部使用的东西来管理不同样式的停止点和开始点,但后来我在网上看到了他们使用 Element 实例的代码示例。
我已经有了自己的数据树结构,需要以不同的字体和颜色显示,遍历它会告诉我在哪里根据需要更改字体或颜色。看起来我将能够通过一系列调用 StyledDocument.setCharacterAttributes 和 setParagraphAttributes 来满足即时需求,而无需亲自接触 Element。
但我的印象是使用 Element 会更高效或更干净。请帮助我正确理解 Element 以及它如何帮助 StyledDocument 和段落的概念,所以即使我现在不使用任何 Elements,我至少会欣赏我所缺少的内容并知道是否和如何在下一个类似的情况下使用它。
I'm trying to understand the purpose of Element and how it can help with manipulating a StyledDocument that is to be displayed in a JEditorPane or JTextPane. I'm also trying to grasp how it relates to the concept of a "paragraph".
The javadoc for javax.swing.text.Element is almost nothing:
public interface Element
Interface to describe a structural piece of a document. It is intended to capture the spirit of an SGML element.
I am very familiar with the concept of an element in HTML and XML, and apparently this is something similar, but I just can't see its purpose as it relates to StyledDocument. At first I figured it was just something the StyledDocument used internally to manage the stop and start points of different styles, but then I saw code examples on the web where they used instances of Element.
I already have my own tree structure of the data I need to display in different fonts and colors, and traversing it will tell me where to change the font or color as needed. It looks like I'll be able to meet the immediate need with a series of calls to StyledDocument.setCharacterAttributes and setParagraphAttributes, without touching Element myself.
But I get the impression that using Element will be more efficient or cleaner. Please help me to get a proper understanding of Element and how it helps with StyledDocument and the concept of a paragrah, so even if I don't use any Elements right now I'll at least appreciate what I'm missing and know if and how to use it for the next similar situation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,DefaultStyledDocument 是一个元素树。有 BranchElement 和 LeafElement。 Leaf 表示一段文本,具有文本属性,如字体大小/样式、字体颜色属性 - 粗体、斜体、下划线等。BachElement 包含 Leaves 或其他分支元素。在最简单的情况下,分支是段落。但根元素也是 BranchElement 的实例。所有元素都可以有自己的属性集。例如,要查找文本的颜色,请询问 LeafElement 的集合。如果未定义颜色,则询问叶子的父元素。
您可以使用它来查看文档如何表示(模型)以及模型如何在视图中表示。
http://java-sl.com/JEditorPaneStructureTool.html
该示例显示了 HTMLDocument 的结构,但您也可以使用相同的代码来查看 StyledEditorKit 的结构
In fact DefaultStyledDocument is a tree of Elements. There are BranchElements and LeafElements. Leaf represents a piece of text with text attributes like font size/style, font color attributes - bold, italic, underline etc. BrachElement contains Leaves or another branch elements. In simplest case the branches are paragraphs. But root Element is also instance of BranchElement. All Elements may have own AttributeSet. To find e.g. color of text LeafElement's set is asked. If color isn't defined the leaf's parent element is asked.
You can use this to see how Document is represented (Model) and How the model is represented in views.
http://java-sl.com/JEditorPaneStructureTool.html
The example shows HTMLDocument's structure but you can use the same code to see structure of StyledEditorKit as well