如何在 Java 中创建一个允许我注释文本的文本编辑器?
我正在开发一个项目,其中包含 JEditorPane 或类似功能中的功能,该功能允许突出显示文本的某些区域并为该文本添加“注释”。这些注释类似于注释。我还会尝试包含显示注释实际文本的工具
提示示例:
Lorem ipsum dolor sat amet、consectetur adipisicing elit、sed do eiusmod tempor incididunt ut Labore et dolore magna aliqua。 Ut enim ad minim veniam,quis nostrud exeritation ullamco labouris nisi ut aliquip ex ea commodo consequat。
我将突出显示第一组文本“consectetur adipisicing elit”,添加一个注释,让我设置突出显示颜色并添加带有注释的工具提示。 “ullamco Laboris nisi ut aliquip”也会发生同样的情况,只是颜色和注释会有所不同。
编辑器应该能够显示这些多重突出显示。
有人能指出我正确的方向吗?我可以使用任何库来更轻松地完成此任务吗?
谢谢,
大卫
I am working on a project that contains a feature in a JEditorPane or similar that allows for the highlighting of certain areas of text and adding "annotations" for that text. These annotations would be similar to a comment. I would also try to include tooltips that show the actual text of the annotation
Example:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
I would highlight the first set of text "consectetur adipisicing elit," add an annotation which would let me set a highlight color and add a tooltip with a comment. The same would happen with "ullamco laboris nisi ut aliquip" except that the color and comment would be different.
The editor should be able to display these multiple highlightings.
Can anyone point me in the right direction? Is there any library I can use to accomplish this easier?
Thanks,
David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于任何
JTextComponent
(JTextField
、JTextPane
、JEditorPane
等),您可以使用setHighLighter() 并向其添加突出显示以指示注释所属的文本范围。您可以使用不同的
HighlightPainter
对象更改颜色。您还可以根据鼠标是否位于突出显示(注释)上方来设置工具提示文本,方法是重写 getToolTipText() 并使用 viewToModel() 来确定在鼠标指向的文本。
For any
JTextComponent
(JTextField
,JTextPane
,JEditorPane
etc) you can usesetHighLighter()
and add highlights to it to indicate the text spans where your annotations belong. You can change the colour using differentHighlightPainter
objects.You could also set the tooltip text according to whether the mouse is over a highlight (annotation) by over-riding
getToolTipText()
and usingviewToModel()
to determine where in the text the mouse is pointing.