如何将 jTextPane 中所有突出显示的单词更改为 jTextField 中给出的单词?
How to change all highlighted words in jTextPane to the word given in jTextField?
我写了一个方法来查找 TextPane(名为 textContent
)中与 TextField1
(您搜索的词)中给出的词相同的所有词,并突出显示它们黄色:
private void findAlleActionPerformed(java.awt.event.ActionEvent evt) {
int j = 0;
int i = 0;
int index = 0;
String search = TextField1.getText();
try{
if(!TextField1.getText().isEmpty()){
while(i != -1){
i = textContent.getText().indexOf(search, j);
if(i == -1)
break;
if(evt.getSource() == findAll || evt.getSource() == findAllButton){
textContent.select(i, i + search.length());
}
Color c = Color.YELLOW;
Style s = textContent.addStyle("TextBackground", null);
StyleConstants.setBackground(s, c);
StyledDocument d = textContent.getStyledDocument();
d.setCharacterAttributes(textContent.getSelectionStart(), textContent.getSelectionEnd() - textContent.getSelectionStart(), textContent.getStyle("TextBackground"), false);
j = i + search.length();
index++;
}
if (index > 0){
textContent.grabFocus();
textContent.setCaretPosition(textContent.getText().indexOf(search, 0) );
if(evt.getSource() == findAll || evt.getSource() == findAllButton){
status.setText("Term: " + TextField1.getText() + ". Number of apperances: " + index);
}
} else {
textContent.grabFocus();
if(evt.getSource() == findAll || evt.getSource() == findAllButton){
status.setText("Term " + search + " was not found.");
}
}
}
} catch (Exception e){
status.setText("Error finding requested term.");
System.err.print(e);
}
}
现在我正在制作另一种方法 changeAllActionperformed()
,用 TextField2
中给出的词替换所有突出显示的词。我试着用 textContent.replaceSelection(TextField2.getText());
这样做,但问题是它把新词放在第一个突出显示的词前面,甚至没有删除它。我想要的是删除所有突出显示的单词并将它们全部替换为 Textfield2
中的新单词。我该怎么做?
为什么不能这么简单?
String replaced = textContent.getText().replace(search, TextField2.getText());
textContent.setText(replaced);
我写了一个方法来查找 TextPane(名为 textContent
)中与 TextField1
(您搜索的词)中给出的词相同的所有词,并突出显示它们黄色:
private void findAlleActionPerformed(java.awt.event.ActionEvent evt) {
int j = 0;
int i = 0;
int index = 0;
String search = TextField1.getText();
try{
if(!TextField1.getText().isEmpty()){
while(i != -1){
i = textContent.getText().indexOf(search, j);
if(i == -1)
break;
if(evt.getSource() == findAll || evt.getSource() == findAllButton){
textContent.select(i, i + search.length());
}
Color c = Color.YELLOW;
Style s = textContent.addStyle("TextBackground", null);
StyleConstants.setBackground(s, c);
StyledDocument d = textContent.getStyledDocument();
d.setCharacterAttributes(textContent.getSelectionStart(), textContent.getSelectionEnd() - textContent.getSelectionStart(), textContent.getStyle("TextBackground"), false);
j = i + search.length();
index++;
}
if (index > 0){
textContent.grabFocus();
textContent.setCaretPosition(textContent.getText().indexOf(search, 0) );
if(evt.getSource() == findAll || evt.getSource() == findAllButton){
status.setText("Term: " + TextField1.getText() + ". Number of apperances: " + index);
}
} else {
textContent.grabFocus();
if(evt.getSource() == findAll || evt.getSource() == findAllButton){
status.setText("Term " + search + " was not found.");
}
}
}
} catch (Exception e){
status.setText("Error finding requested term.");
System.err.print(e);
}
}
现在我正在制作另一种方法 changeAllActionperformed()
,用 TextField2
中给出的词替换所有突出显示的词。我试着用 textContent.replaceSelection(TextField2.getText());
这样做,但问题是它把新词放在第一个突出显示的词前面,甚至没有删除它。我想要的是删除所有突出显示的单词并将它们全部替换为 Textfield2
中的新单词。我该怎么做?
为什么不能这么简单?
String replaced = textContent.getText().replace(search, TextField2.getText());
textContent.setText(replaced);