java实现查找替换功能
时间:2021-06-10 08:22:46|栏目:JAVA代码|点击: 次
本文实例为大家分享了java实现查找替换功能的具体代码,供大家参考,具体内容如下
查找
if(searchTxt.getText().equals("")){
JOptionPane.showMessageDialog(null, "查找内容不能为空!");
}else if(!searchTxt.getText().equals("")){
//searchDialog.dispose();
if(fileChoose.focus == 1){
fileChoose.tp1.requestFocus(true);
str = fileChoose.tp1.getText();
startPosition = str.indexOf(searchTxt.getText());
endPosition = startPosition + searchTxt.getText().length();
fileChoose.tp1.select(startPosition, endPosition);
newEndPosition = endPosition;
}else if(fileChoose.focus == 2){
fileChoose.tp2.requestFocus(true);
str = fileChoose.tp2.getText();
startPosition = str.indexOf(searchTxt.getText());
endPosition = startPosition + searchTxt.getText().length();
fileChoose.tp2.select(startPosition, endPosition);
newEndPosition = endPosition;
}
}
查找下一处
nextTemp = newEndPosition;
if(fileChoose.focus == 1){
strAll = fileChoose.tp1.getText();
fileChoose.tp1.select(nextTemp, strAll.length());//选中所有未查找的字符串
nextStr = fileChoose.tp1.getSelectedText();
newStartPosition = nextStr.indexOf(searchTxt.getText()) + nextTemp;//在未查找的字符串中查找对应字符在tp1中的位置
newEndPosition = newStartPosition + searchTxt.getText().length();
fileChoose.tp1.select(newStartPosition, newEndPosition);//找到文本,选择文本
}else if(fileChoose.focus == 2){
strAll = fileChoose.tp2.getText();
fileChoose.tp2.select(nextTemp, strAll.length());//选中所有未查找的字符串
nextStr = fileChoose.tp2.getSelectedText();
newStartPosition = nextStr.indexOf(searchTxt.getText()) + nextTemp;//在未查找的字符串中查找对应字符在tp1中的位置
newEndPosition = newStartPosition + searchTxt.getText().length();
fileChoose.tp2.select(newStartPosition, newEndPosition);//找到文本,选择文本
}
替换
if(fileChoose.focus == 1){
fileChoose.tp1.requestFocus(true);
str = fileChoose.tp1.getText();
startPosition = str.indexOf(searchTxt.getText());
endPosition = startPosition + searchTxt.getText().length();
fileChoose.tp1.replaceSelection(replaceTxt.getText());
newEndPosition = endPosition;
}else if(fileChoose.focus == 2){
fileChoose.tp2.requestFocus(true);
str = fileChoose.tp2.getText();
startPosition = str.indexOf(searchTxt.getText());
endPosition = startPosition + searchTxt.getText().length();
fileChoose.tp2.replaceSelection(replaceTxt.getText());
newEndPosition = endPosition;
}
替换下一处
if(fileChoose.focus == 1){
fileChoose.tp1.requestFocus(true);
nextTemp = newEndPosition;
strAll = fileChoose.tp1.getText();////////////////////////////////
fileChoose.tp1.select(nextTemp, strAll.length());/////////////////////////////
nextStr = fileChoose.tp1.getSelectedText();
newStartPosition = nextStr.indexOf(searchTxt.getText()) + nextTemp;
newEndPosition = newStartPosition + searchTxt.getText().length();
fileChoose.tp1.select(newStartPosition, newEndPosition);
fileChoose.tp1.replaceSelection(replaceTxt.getText());
}else if(fileChoose.focus == 2){
fileChoose.tp2.requestFocus(true);
nextTemp = newEndPosition;
strAll = fileChoose.tp2.getText();
fileChoose.tp2.select(nextTemp,strAll.length());
nextStr = fileChoose.tp2.getSelectedText();
newStartPosition = nextStr.indexOf(searchTxt.getText()) + nextTemp;
newEndPosition =newStartPosition + searchTxt.getText().length();
fileChoose.tp2.select(newStartPosition, newEndPosition);
fileChoose.tp2.replaceSelection(replaceTxt.getText());
}


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




