突出显示用户搜索的 jTextField 中的所有单词
Highlight all words in jTextField that the user puts into search for
嘿,下面的所有代码都可以突出显示要搜索的提供的单词,但如果句子中的单词不止一 (1) 个,那么它将找不到它。它只是在找到一 (1) 个匹配项时停止。
private JFrame frame;
private static JTextField textField;
private static JTextField txtWorld;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CaretDemo window = new CaretDemo();
window.frame.setVisible(true);
String text = "hello world. How are you?";
textField.setText(text);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public CaretDemo() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(21, 11, 350, 36);
frame.getContentPane().add(textField);
textField.setColumns(10);
txtWorld = new JTextField();
txtWorld.setColumns(10);
txtWorld.setBounds(21, 156, 350, 36);
frame.getContentPane().add(txtWorld);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Highlighter highlighter = textField.getHighlighter();
HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.pink);
int p0 = textField.getText().indexOf(txtWorld.getText());
int p1 = p0 + txtWorld.getText().length();
try {
highlighter.removeAllHighlights();
highlighter.addHighlight(p0, p1, painter);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
});
btnNewButton.setBounds(221, 203, 128, 29);
frame.getContentPane().add(btnNewButton);
}
当 运行 并提供单词 "world" 进行查找时,上面的代码看起来像这样。
但是现在如果我添加到句子中并添加另一个 "world" 然后点击按钮这就是它的样子:
如您所见,它仍然有相同的突出显示词,但没有突出显示第二个 (2) "world"。
更新 1
我尝试使用正则表达式来循环查找所需的单词。
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("hi");
String theSentence = txtTheSentence.getText();
String WordToFind = txtWordToFind.getText();
Highlighter h = txtWordToFind.getHighlighter();
Pattern pattern = Pattern.compile("\b"+WordToFind+"\b", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(theSentence);
if(matcher.find()){
String extractedWord = matcher.group(0);
System.out.println(extractedWord);
}
}
});
但这并没有产生一个找到的词。
it still has the same highlighted word without also highlighting the second (2) "world" -
嗯,这正是您的代码所做的。
首先删除所有高亮显示,然后添加回一个高亮显示。
如果要突出显示多个单词,则需要:
- 先删除所有高亮,然后
- 编写一个循环并处理字符串中的所有文本。
找到匹配的文本后,您可以使用 String.indexOf(text, fromIndex)
方法从新索引继续搜索。
终于明白了!
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("hi");
String theSentence = txtTheSentence.getText();
String theWord = txtWordToFind.getText();
Highlighter h = txtWordToFind.getHighlighter();
Pattern pattern = Pattern.compile(theWord, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(theSentence);
while (matcher.find()) {
String extractedWord = matcher.group();
System.out.println(extractedWord);
}
}
嘿,下面的所有代码都可以突出显示要搜索的提供的单词,但如果句子中的单词不止一 (1) 个,那么它将找不到它。它只是在找到一 (1) 个匹配项时停止。
private JFrame frame;
private static JTextField textField;
private static JTextField txtWorld;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CaretDemo window = new CaretDemo();
window.frame.setVisible(true);
String text = "hello world. How are you?";
textField.setText(text);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public CaretDemo() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(21, 11, 350, 36);
frame.getContentPane().add(textField);
textField.setColumns(10);
txtWorld = new JTextField();
txtWorld.setColumns(10);
txtWorld.setBounds(21, 156, 350, 36);
frame.getContentPane().add(txtWorld);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Highlighter highlighter = textField.getHighlighter();
HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.pink);
int p0 = textField.getText().indexOf(txtWorld.getText());
int p1 = p0 + txtWorld.getText().length();
try {
highlighter.removeAllHighlights();
highlighter.addHighlight(p0, p1, painter);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
});
btnNewButton.setBounds(221, 203, 128, 29);
frame.getContentPane().add(btnNewButton);
}
当 运行 并提供单词 "world" 进行查找时,上面的代码看起来像这样。
但是现在如果我添加到句子中并添加另一个 "world" 然后点击按钮这就是它的样子:
如您所见,它仍然有相同的突出显示词,但没有突出显示第二个 (2) "world"。
更新 1
我尝试使用正则表达式来循环查找所需的单词。
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("hi");
String theSentence = txtTheSentence.getText();
String WordToFind = txtWordToFind.getText();
Highlighter h = txtWordToFind.getHighlighter();
Pattern pattern = Pattern.compile("\b"+WordToFind+"\b", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(theSentence);
if(matcher.find()){
String extractedWord = matcher.group(0);
System.out.println(extractedWord);
}
}
});
但这并没有产生一个找到的词。
it still has the same highlighted word without also highlighting the second (2) "world" -
嗯,这正是您的代码所做的。
首先删除所有高亮显示,然后添加回一个高亮显示。
如果要突出显示多个单词,则需要:
- 先删除所有高亮,然后
- 编写一个循环并处理字符串中的所有文本。
找到匹配的文本后,您可以使用 String.indexOf(text, fromIndex)
方法从新索引继续搜索。
终于明白了!
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("hi");
String theSentence = txtTheSentence.getText();
String theWord = txtWordToFind.getText();
Highlighter h = txtWordToFind.getHighlighter();
Pattern pattern = Pattern.compile(theWord, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(theSentence);
while (matcher.find()) {
String extractedWord = matcher.group();
System.out.println(extractedWord);
}
}