【问题标题】:Highlighting String in JTextArea [duplicate]在 JTextArea 中突出显示字符串 [重复]
【发布时间】:2012-11-27 13:18:09
【问题描述】:

可能重复:
Highlighting few of the words of a text file opened in a frame

我在JTextArea 中找到一个字符串,一旦找到该字符串,我想用某种颜色突出显示该字符串。我一直在互联网上寻找解决方案,但找不到答案。

如何突出显示JTextArea 中的字符串?

【问题讨论】:

  • 然后看看thisthis
  • JTextArea 用于纯文本(所有一种字体、样式和颜色)。对于带样式的文本,请改用 JEditorPaneJTextPane
  • 参见herehere 获取一些示例。也为他的例子 +1 @MadProgrammer :)
  • @AndrewThompson 或者你可以作弊并使用文本选择:P
  • @MadProgrammer 是的,我不久前查看了您的聪明示例。 +1

标签: java swing highlight jtextarea


【解决方案1】:

您需要为此使用HighlighterThisOracle 教程应该让你走上正轨。

编辑:

entry.getDocument().addDocumentListener(this);:这会将当前类附加为特定对象的事件处理程序。很有可能你可以不用这行代码。

hilit.addHighlight(index, end, painter);:这应该会突出显示文本。

entry.setBackground(entryBg);:为给定项目设置背景。

我建议你复制教程中编写的代码并从那里开始。

【讨论】:

  • 谢谢。你能告诉我以下代码行做什么:entry.getDocument().addDocumentListener(this); hilit.addHighlight(索引,结束,画家); textArea.setCaretPosition(end); entry.setBackground(entryBg);
  • @Alfred:我已经修改了我的答案。恐怕除了你必须弄脏你的手之外别无他法:)
猜你喜欢
  • 1970-01-01
  • 2012-09-02
  • 2013-10-25
  • 1970-01-01
  • 2012-06-18
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
相关资源
最近更新 更多