- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是java新手,正在研究java swing GUI。最近读到一篇帖子: Centering Text in a JTextArea or JTextPane - Horizontal Text Alignment
里面的解决方案工作得很好,但我有一些概念性问题要问。
我在oracle网站上看了接口(interface)和类的介绍。它表示该接口(interface)包含一组空体方法,那么实现该接口(interface)的类需要声明该接口(interface)中提到的所有方法才能成功编译。
我的问题来了:看完文档,我知道StyledDocument是一个接口(interface),但是下面的代码是什么意思呢?
StyledDocument doc = textPane.getStyledDocument();
我的解释是,我猜 JTextPane 在内部实现了 StyledDocument,因此这行代码将接收现有的 StyledDocument (但它不应该被称为实例,因为我们无法创建接口(interface)实例,我该如何描述它?)。如果这是真的,那么 JTextPane 应该具有在 StyledDocument 接口(interface)中定义的所有方法。
我在上面的段落中正确吗?
然后,我尝试不写这两行代码:
StyledDocument doc = textPane.getStyledDocument();
doc.setParagraphAttributes(0, doc.getLength(), center, false);
但是我直接使用了:
textPane.setParagraphAttributes(center, false);
这也很有效。
那么,这两种实现之间有什么区别吗?
我的代码是这样做的好习惯吗?
非常感谢您的帮助!
最佳答案
我认为您陷入了多态性的概念,看看 Polymorphism 上的踪迹对于初学者。
My interpretation is that, I guess that a JTextPane implements the StyledDocument internally so that this line of code is to receive the existing StyledDocument (But it should not be called an instance as we could not create instance of interface, How should I describe it?). If this is true, then JTextPane should have all methods defined in the StyledDocument interface.
没有。 getStyledDocument
方法返回一个实现 StyledDocument
接口(interface)的对象。 JTextPane
不直接实现此功能,而是将要求委托(delegate)给实现 StyledDocument
接口(interface)的对象实例。
它们共同提供了显示样式文本的方法。这是Model-View-Controller的一个概念。范例,其中非可视功能(模型或 StyledDocument
)与 View (JTextPane
)分离
Then, I tried not to write this two lines of code:
StyledDocument doc = textPane.getStyledDocument();
doc.setParagraphAttributes(0, doc.getLength(), center, false);But I directly used:
textPane.setParagraphAttributes(center, false);
And this also worked perfectly.
So, are there any differences between the two implementations?
是和否。 setParagraphAttributes
将功能委托(delegate)给 StyledDocument
,如下面摘自 JTextPane
的代码片段所示:
public void setParagraphAttributes(AttributeSet attr, boolean replace) {
int p0 = getSelectionStart();
int p1 = getSelectionEnd();
StyledDocument doc = getStyledDocument();
doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
}
它只是一种方便的方法,让您的生活变得更简单
Is my code a good practice to do so?
我认为使用提供的功能来实现您的目标没有问题。
关于java - 关于StyledDocument和JTextPane的关系以及接口(interface)的正确使用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31823467/
对于 NetBeans 插件,我想更改具有特定字符串和特定字符集的文件(在 NetBeans 编辑器中打开)的内容。为了实现这一点,我使用 EditorCookie 打开文件(数据对象),然后通过将不
我目前正在使用 JSwing 编写一个非常简单的程序。 JScrollPane ("textPane") 内有一个 JTextArea ("textArea")。我设法使用名为 doc 的 Style
虽然有相当多的空闲时间可以使用,但我遇到了有关将 StyledDocument 作为 JTextPane 一部分的问题。当文档的总大小达到值 x (在我的例子中为 10.000)时,我想删除文本的第一
我有一个 DefaultStyledDocument,里面有格式化文本。我还有一个函数,它使用模式匹配器分割内容(作为纯文本)。 我需要一个函数,它从分割的输出生成新的完整的 DefaultStyle
StyledDocument包含各种设置样式的方法。喜欢setCharacterAttributes . 但我看不到任何删除样式的方法。 有没有? 最佳答案 “清除”样式是不可能的。应该使用以下技术获
我正在尝试创建一个文本编辑器。我正在使用带有 StyledDocument 的 JTextPane。我正在尝试实现的是一种更改所选文本属性的方法。 这按以下方式工作:用户输入所需的文本。之后,他可以更
JTextPane pane = new JTextPane(); pane.setText("some text"); add(pane); pane.addStyle("red", null);
我似乎在替换 JTextPane 中的文本时遇到了障碍。我有几个 JTextPanes 需要更改现有文本。我可以使用初始文本执行此操作,但是当我调用更改文档时,我在调用中看到此更改,但面板不会更新。
我正在创建一个基本的 Swing 应用程序。我有 jTextPane 和一个按钮。如果用户在 jTextPane 中选择文本并按下按钮,它会将所选文本加粗。这是通过修改 StyleDocument 来
我使用两个 DefaultStyledDocument 编写了两个 Pane diff 类。 现在我想让用户就地更改用于显示删除和插入的颜色(以及粗体),而不需要重新比较或保留中间比较结果。 我面临的
这对我来说有点矫枉过正..我正在使用 JTextPane 进行聊天,我在那里有颜色..我想要的是,引用一个元素改变它的颜色..我正在使用 StyledDocument,我不知道该怎么做...... 提
与 JTextPane 关联的 StyledDocument 使用什么字体?默认情况下,它使用与 JTextPane 相同的字体吗?特别是,我想知道字体大小。 最佳答案 StyledDocument
我试图了解 Element 的用途,以及它如何帮助操作要在 JEditorPane 或 JTextPane 中显示的 StyledDocument。我还试图了解它与“段落”概念的关系。 javax.s
我有一个 JTextPane ,我想使用 StyledDocument 在其中输出文本.这是我的 StyledDocument对象: StyledDocument dox = (StyledDocum
当鼠标悬停在文本的不同部分上时,我想突出显示它们。我目前正在将 styleddocument 与 jTextPane 一起使用。有人可以帮助我定义样式,以便在鼠标悬停时可以突出显示各个字符串/段落吗?
我不知道我正在尝试做的事情是否可行。 我有一个控制台,我想在其中附加这样声明的格式化文本: private final JTextPane statusText = new JTextPane();
我不知道这是否可行,但我想做的是将样式文档(用户可以更改文本:粗体、下划线、斜体和 3 种字体大小)保存在 .doc 文件中 -这样他就可以稍后使用任何其他支持样式文本的文本编辑器打开它。 我写了下面
我一直在使用 StyledDocument 的 setCharacterAttributes 来更改 JTextPane 中特定单词或部分的颜色。 但后来我注意到这里有一些答案,他们使用 Highli
我有一个 JTextPane,其中包含我从文件加载的 html 内容。一些评论标签是占位符,我想在它们之前插入组件。我正在使用正则表达式匹配器查找这些标签,我可以使用 matcher.start()
我将此文本设置为 JTextPane,内容类型为 text/html。 Test document This is new more title test
我是一名优秀的程序员,十分优秀!