在 JTextField/JTextPane/ 中使单词具有不同的颜色?
我正在尝试但未能理解如何使用 Java 的文本编辑器组件在插入文本时为文本着色。我不想要或不需要一个功能齐全的语法突出显示库。
基本上,我有一个 JTextField (或其他一些 JText... 组件)和一个单词列表。我希望列表中出现的字段中的任何单词都为红色,其余单词为绿色。例如,如果“火”在列表中,“冷杉”将显示为绿色,“火”将显示为红色。
我尝试使用 JTextPane 和 DefaultStyledDocument,使用 KeyListener 遍历文档中的文本,并使用 AbstractStyledDocument.replace 将现有单词替换为具有正确属性的版本。这没有起到任何作用。我做错了什么?
I'm trying and failing to understand how to use Java's text editor components to colorize text as you insert it. I don't want or need a fully featured syntax highlighting library.
Basically, I have a JTextField (or some other JText... component), and a list of words. I want any words in the field that appear in the list to be red, and the rest of the words be green. So for example, if "fire" is in the list, "fir" would appear green and "fire" would appear red.
I've tried using a JTextPane and a DefaultStyledDocument, using a KeyListener to go over the text in the document and using AbstractStyledDocument.replace to replace the existing words with versions that have the correct attributes. This didn't do anything. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JTextPane
和JTextField
都无法呈现格式化文本,即具有多种格式的文本。对于类似写字板或 HTML 中的文本编辑器功能,要使用的组件是JEditorPane
或其后代JTextPane
。您可以做的最简单的事情是将 JEditorPane 的 ContentType 设置为“text/html”,并将其文本设置为包含 HTML 的字符串。 Java 结构化文本组件对 HTML 的处理能力令人惊讶。您可以显示表格和/或 DIV,并且支持大部分 CSS2。最简单的做法是内联样式,但您甚至可以执行外部样式
href
。如果您想以编程方式获得奇特的效果,您可以访问
DocumentModel
并从文本范围创建文本,每个文本范围都有自己的格式。 DocumentModel 的工作原理本质上就像一个可编程文本编辑器。编辑:重新阅读您的问题,我发现我的答案并没有完全解决它。由于您想要多色文本 JEditorPane 是您唯一的选择;但是,您不仅需要通过 HTML 等管道输入预先着色的文本,还必须在文档模型上放置一个侦听器来捕获键入时引入的更改;每次文档更改后,您都需要检查文本(再次来自文档模型)以查找应该或不应该突出显示的文本,并且您需要将格式应用于某些文本运行。
细节很复杂,但这应该可以帮助您开始。
NeitherJTextPane
norJTextField
isn't able to present formatted text, i.e text having more than one format. For text-editor-like capabilities like you'd find in WordPad or HTML, the component to use is theJEditorPane
or its descendant,JTextPane
.The simplest thing you can do is set the ContentType of the JEditorPane to "text/html" and simply set its text to a string containing HTML. The Java structured text components are surprisingly competent with HTML; you can display tables and/or DIVs, and there is support for much of CSS2. Simplest to do your styles inline, but you can even do external style
href
s.If you want to get fancy programmatically, you can access the
DocumentModel
and create text from spans of text each having their own formatting. The DocumentModel works essentially like a programmable text editor.EDIT: Re-reading your question, I see my answer doesn't quite address it. Since you want multi-colored text JEditorPane is your only option; but rather than just piping in pre-colored text via HTML or such, you'll have to put a listener on your document model to catch changes introduced when you type; and after every document change you'll want to examine the text (again from the Document model) for text that should or should not be highlighted, and you'll want to apply formatting to certain runs of text.
There are devils in the details, but this should get you started.