Java Swing中元素的用途及其与StyledDocument和段落的关系(Purpose of Element in java Swing and its relationship to Sty

编程入门 行业动态 更新时间:2024-10-18 01:27:47
Java Swing中元素的用途及其与StyledDocument和段落的关系(Purpose of Element in java Swing and its relationship to StyledDocument and paragraphs)

我试图理解Element的用途以及它如何帮助操作将在JEditorPane或JTextPane中显示的StyledDocument 。 我也试图了解它与“段落”概念的关系。

javax.swing.text.Element的javadoc几乎没有:

公共接口元素 用于描述文档结构片段的界面。 它旨在捕捉SGML元素的精神。

我非常熟悉HTML和XML中元素的概念,显然这是类似的,但我无法看到它与StyledDocument相关的目的。 起初我认为它只是StyledDocument内部用来管理不同样式的停止点和起点的东西,但后来我在Web上看到了使用Element实例的代码示例。

我已经拥有自己的数据树结构,我需要以不同的字体和颜色显示,遍历它会告诉我在哪里根据需要更改字体或颜色。 看起来我能够通过对StyledDocument.setCharacterAttributessetParagraphAttributes的一系列调用满足当前需求,而不需要自己触摸Element。

但我得到的印象是使用Element会更高效或更清洁。 请帮助我正确理解Element以及它如何帮助StyledDocument和paragrah的概念,所以即使我现在不使用任何元素,我至少会欣赏我所缺少的东西,并且知道是否和如何将它用于下一个类似的情况。

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.

最满意答案

实际上,DefaultStyledDocument是Elements的一棵树。 有BranchElements和LeafElements。 Leaf表示一段文本,其中包含文本属性,如字体大小/样式,字体颜色属性 - 粗体,斜体,下划线等.BrachElement包含Leaves或其他分支元素。 在最简单的情况下,分支是段落。 但是根元素也是BranchElement的实例。 所有元素都可以拥有自己的AttributeSet。 要查找例如文本的颜色,请询问LeafElement的集合。 如果未定义颜色,则会询问叶子的父元素。

您可以使用它来查看Document的表示方式(Model)以及如何在视图中表示模型。 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

更多推荐

本文发布于:2023-08-01 02:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353943.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:段落   用途   元素   关系   StyledDocument

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!