gpt4 book ai didi

java - 突出显示 JTextArea

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:56:33 24 4
gpt4 key购买 nike

我有一个问题:

        int index = text.indexOf(myWord);

while (index >= 0) {
int len = pattern.length();
hl.addHighlight(index, index + len, DefaultHighlighter.DefaultPainter);
index = text.indexOf(pattern, index + len);
}

当我到达行尾时高亮仍然启用,如何禁用高亮?上面代码的结果:

enter image description here

感谢您的回答,但问题是:

enter image description here

黑线是我点击并开始输入的地方,这只是一个错误。

最佳答案

好吧,我不得不对您的变量名做一些假设,我希望我做对了。基本上你的代码对我有用,但我不明白第一行:

int index = text.indexOf(myWord);

我认为应该是:

int index = text.indexOf(pattern);

无论如何,我编写了这段代码并且它对我有用:

String pattern = "<aa>";
String text = textArea.getText();
int index = text.indexOf(pattern);
while(index >= 0){
try {
hl.addHighlight(index, index + pattern.length(), DefaultHighlighter.DefaultPainter);
index = text.indexOf(pattern, index + pattern.length());
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}

更新

Look when you click on the end of marked text and start typing the text will be also marked- it is a my bug.

抱歉,我之前没有看到。我不认为你可以停止荧光笔,但你可以使用 CaretListener 并在那里做一些事情。这样,如果您输入与您的模式匹配的新字符串,它也会突出显示:

textArea.addCaretListener(new CaretListener() {            
@Override
public void caretUpdate(CaretEvent e) {
if(e.getMark() == e.getDot()){
Highlighter hl = textArea.getHighlighter();
hl.removeAllHighlights();

String pattern = "<aa>";
String text = textArea.getText();
int index = text.indexOf(pattern);
while(index >= 0){
try {
Object o = hl.addHighlight(index, index + pattern.length(), DefaultHighlighter.DefaultPainter);
index = text.indexOf(pattern, index + pattern.length());
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
}
}
});

注意 e.getMark() == e.getDot() 表示“没有文本选择”基于 CaretEvent.getDot()CaretEvent.getMark() java文档。否则你不应该突出显示所选文本以外的任何内容。

截图

enter image description here

更新的 SSCCE

这是一个完整的 SSCCE测试它:

import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class Demo {

private void initGUI(){

final JTextArea textArea = new JTextArea();

textArea.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
if(e.getMark() == e.getDot()){
Highlighter hl = textArea.getHighlighter();
hl.removeAllHighlights();

String pattern = "<aa>";
String text = textArea.getText();
int index = text.indexOf(pattern);
while(index >= 0){
try {
Object o = hl.addHighlight(index, index + pattern.length(), DefaultHighlighter.DefaultPainter);
index = text.indexOf(pattern, index + pattern.length());
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
}
}
});

JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(300,200));

JPanel content = new JPanel(new FlowLayout());
content.add(scrollPane);

JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setContentPane(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}


public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().initGUI();
}
});
}

}

关于java - 突出显示 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19427321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com